飞机游戏初步

步骤

1:

创建 hellogame项目

tools->cocos2d-console->bin->shift+右键->在此处打开命令窗口->路径pythoncocos.py new hellogame –p com.game.hellogame -lcpp -d game

2:在gameScene.h中(第一个场景)

<pre name="code" class="cpp">#ifndef __GAME_SCENE_H__    <strong>//大写,与项目名字一样</strong>
#define __GAME_SCENE_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

class <strong>GameScene </strong>: public cocos2d::Layer
{
public:
	
	static cocos2d::Scene* createScene();
	SimpleAudioEngine*Sound = SimpleAudioEngine::getInstance();
	
	virtual bool init(); <strong> //虚函数</strong>

   <strong>  //对cpp中的相对应函数进行调用</strong>
	void onBegin(cocos2d::Ref* pSender);

	
	CREATE_FUNC(<strong>GameScene</strong>);
};

#endif

;
 


3:在gameScene.cpp中

</pre><pre name="code" class="cpp">#include "HelloWorldScene.h"
#include"SimpleAudioEngine.h"
#include "GameScene.h"  <strong>  //必要的引用头文件</strong>

USING_NS_CC;

<strong>using namespace std;</strong>

Scene* GameScene::createScene()
{

	auto scene = Scene::create();

	auto layer = GameScene::create();

	scene->addChild(layer);

	return scene;
}

bool GameScene::init()
{

	if (!Layer::init())
	{
		return false;
	}

	Sound->playBackgroundMusic("bgm1.mp3", true);

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	
	auto background = Sprite::create("back1.png", Rect(0, 0, 440, 664));
	background->setAnchorPoint(Vec2(0.5, 0.5));
	background->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(background);



	//按钮设置添加start和quit按钮,具体参考cocos的API:  MenuTest
	auto start = MenuItemFont::create("start", CC_CALLBACK_1(GameScene::onBegin, this));
	start->setTag(1);
	auto quit = MenuItemFont::create("quit", CC_CALLBACK_1(GameScene::onBegin, this));
	quit->setTag(2);
	auto menu = Menu::create(start,quit,nullptr);
	menu->alignItemsVertically();
	this->addChild(menu);
	menu->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 - 150));

	

	return true;
}



void GameScene::onBegin(Ref* pSender)
{
	
	auto ss = (MenuItemFont*)pSender;
	int str = ss->getTag();
	if (str==1)
	{
		auto scene = HelloWorld::createScene();
		Director::getInstance()->replaceScene(scene);
	}
	else
	{
		Director::getInstance()->end();
	}
}

上面的是一个开始类的创建,后面的类的创建和这个类似,可以对照修改


4:在HelloWorldScene.cpp中(第二场景)

</pre><pre name="code" class="cpp">#include "HelloWorldScene.h"
#include"SimpleAudioEngine.h"
#include "Gamescene.h"
#include "Plane1.h"
#include "Bullet.h"


USING_NS_CC;

Scene* HelloWorld::createScene()
{
    
    auto scene = Scene::create();
   
    auto layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	Sound->playBackgroundMusic("bgm.mp3",true);

	//按钮设置添加start和quit按钮,具体参考cocos的API:  MenuTest
	/*auto start = MenuItemFont::create("start", CC_CALLBACK_1(HelloWorld::onStart, this));
	start->setName("start");
	auto quit = MenuItemFont::create("quit", CC_CALLBACK_1(HelloWorld::onStart, this));
	start->setName("quit");
	auto menu = Menu::create(start, quit, nullptr);
	menu->alignItemsVertically();
	this->addChild(menu);*/


	
	//加载图片,加载两张相同图片为了背景的上下移动交换
	auto background1 = Sprite::create("back.png");   
	auto background2 = Sprite::create("back.png");

	//设置Tag值的第一种方式,中间的0是层级
	this->addChild(background1,0,1);  
	this->addChild(background2,0,2);

	background1->setAnchorPoint(Vec2(0, 0));  //锚点设置
	//background1->setPosition(Vec2(visibleSize.width/2 , visibleSize.height/2));
	background2->setAnchorPoint(Vec2(0, 0));
	background2->setPosition(Vec2(0 , visibleSize.height));
	//background1->setTag(1);        //给图片设置随意的Tag值的第二种方式
	//background1->setTag(2);
	

	//退出按键
	auto quit = MenuItemFont::create("quit", CC_CALLBACK_1(HelloWorld::onStart, this));
	quit->setTag(2);
	auto menu = Menu::create(quit, nullptr);
	menu->alignItemsVertically();
	this->addChild(menu);
	menu->setPosition(Vec2(visibleSize.width/2+150, visibleSize.height/2+300));
	

	//计时器,具体参考API:schedule
	this-> schedule(CC_SCHEDULE_SELECTOR(HelloWorld::onLoad), 0.1f);

	//动态创建plane1.h类的对象
	Planes *p = new Planes;
	this->addChild(p,1,4);


	//动态创建Bullet.h类的对象
	Bullet *p1 = new Bullet;
	this->addChild(p1, 1, 3);

    return true;
}
//结合计时器
void HelloWorld::onLoad(float dt){
	//设置一个精灵b获取Tag值,从而获取background背景
	auto b1 = this->getChildByTag(1); 
	auto b2 = this->getChildByTag(2);

	//获取舞台大小
	Size visibleSize = Director::getInstance()->getVisibleSize();  

	//自己设置的锚点的位置以每0.5秒下移20的速度移动
	b1->setPositionY(b1->getPositionY() - 20);     
	b2->setPositionY(b2->getPositionY() - 20);

	//判断设置的锚点的位置与y轴的关系,此处是移动到y轴下方时
	if (b1->getContentSize().height + b1->getPositionY()<=0)   
	{
		b1->setPositionY(b1->getPositionY() + b1->getContentSize().height * 2);  
	}         //getPosition为图片的y轴,getContentSize为舞台的大小
	if (b2->getContentSize().height + b2->getPositionY() <= 0)   //判断设置的锚点的位置与y轴的关系,此处是移动到y轴下方时
	{
		b2->setPositionY(b2->getPositionY() + b2->getContentSize().height * 2);
	}
}

//按quit键时,切换为场景1;
void HelloWorld::onStart(Ref* pSender)
{
   // Director::getInstance()->end();
	auto dd = (MenuItemFont*)pSender;
	int str = dd->getTag();
	if (str == 2)
	{
		auto scene = GameScene::createScene();
		Director::getInstance()->replaceScene(scene);
	}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

5:在HelloWorldScene.h中

</pre><pre name="code" class="cpp">#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

//根据API,使用音乐必须引用的代码
using namespace CocosDenshion;

class HelloWorld : public cocos2d::Layer
{
public:
  
    static cocos2d::Scene* createScene();
	SimpleAudioEngine*Sound = SimpleAudioEngine::getInstance();
    
    virtual bool init();
    
  
    void menuCloseCallback(cocos2d::Ref* pSender);

	//对void HelloWorld::onStart(Ref* pSender)函数的引用
	void onStart(cocos2d::Ref* pSender);

	void onLoad(float dt);
    
   
    CREATE_FUNC(HelloWorld); //注意和类名保持一致
};

#endif   // __HELLOWORLD_SCENE_H__

</pre><p><strong><span style="font-size:14px;"><span style="color:#ff0000;">6:主机类(Plane1.cpp)</span></span></strong></p><p><strong><span style="font-size:14px;"><span style="color:#ff0000;"></span></span></strong><pre name="code" class="cpp">#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include  "Plane1.h"

USING_NS_CC;
using namespace std;

//动态创建对象要在构造函数里面调用
Planes::Planes(){
	init();
}


bool Planes::init()
{

	if (!Layer::init())
	{
		return false;
	}

	//舞台
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//主战机的出场和位置
	auto plane = Sprite::create("plane.png");
	this->addChild(plane, 1, 1);
	plane->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2-250));

	return true;
}

7:主机类(Plane1.h)

#ifndef __PLANE_SCENE_H__
#define __PLANE_SCENE_H__


#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "GameScene.h"

using namespace CocosDenshion;

class Planes : public cocos2d::Layer
{
public:
	Planes();    //构造函数
	virtual bool init();

	
	//void onPlane(cocos2d::Ref* pSender);


	CREATE_FUNC(Planes);
};

#endif

8:子弹类(Bullet.cpp)

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include  "Bullet.h"

USING_NS_CC;
using namespace std;

//动态创建对象要在构造函数里面调用
Bullet::Bullet(){
	init();
}


bool Bullet::init()
{

	if (!Layer::init())
	{
		return false;
	}
	//获取舞台大小和图片素材的原点坐标
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//计时器
	this->schedule(CC_SCHEDULE_SELECTOR(Bullet::onFly), 0.2f);

	return true;
}

//配合计时器,子弹的发射(MoveTo,MoveBy)
void Bullet::onFly(float bt){
	//获取舞台大小
	Size visibleSize = Director::getInstance()->getVisibleSize(); 
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//子弹的出场,位置还有移动
	auto bullet = Sprite::create("bullet1.png");
	//子弹的声音
	Sound->playBackgroundMusic("bgm3.wav");
	this->addChild(bullet, 1);
	bullet->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 - 245));
	auto mv = MoveBy::create(1, Vec2(0, visibleSize.height));
	bullet->runAction(mv);
}

9:子弹类(Bullet.h

#ifndef __BULLET_SCENE_H__
#define __BULLET_SCENE_H__


#include "cocos2d.h"
#include "SimpleAudioEngine.h"  //使用声音需要引用的

#include "GameScene.h"    //子弹是在第二场景中的,所以引用

using namespace CocosDenshion; <span style="color: rgb(255, 0, 0);font-size:14px;"></span><pre name="code" class="cpp">         //使用声音需要引用的
class Bullet : public cocos2d::Layer{public:Bullet(); //构造函数virtual bool init();

 


	//cpp文件中有参数的函数的申明
	void onFly(float bt);

	static cocos2d::Scene* createScene();
	SimpleAudioEngine*Sound = SimpleAudioEngine::getInstance();

	CREATE_FUNC(Bullet);
};

#endif










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值