今天,我是要告诉大家如何实现创建背景、飞机、***精灵,并且然后他们动起来,然后做一个碰撞测试,当***和敌方飞机碰撞时就销毁精灵并且加一个爆炸的精灵。下面我来说cocos2dx之飞机大战简单版。
-
创建背景、飞机、***精灵
先在GameScene.h中添加以下成员
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
GameScene();
~GameScene();
void addMonster();
void addBullet1();
void GameLogic(float dt);
void GameLogicaddBullet1(float dt);
void GameLogicaddBullet2(float dt);
void spriteMoveFinished(CCNode *sender);
void keyArrowClicked(int arrow);
void ccTouchesBegan(cocos2d::CCSet *pTouches,cocos2d::CCEvent *pEvent);
void ccTouchesMoved(cocos2d::CCSet *pTouches,cocos2d::CCEvent *pEvent);
cocos2d::CCArray *_monsters;
cocos2d::CCArray *_bullets;
cocos2d::CCSprite *player;
cocos2d::CCSprite *bgSprite1;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
int bgHeight;
int monsterSpeed;
|
在GameScene::init()中添加以下代码
1
2
|
visibleSize = CCDirector::sharedDirector()->getVisibleSize();
origin = CCDirector::sharedDirector()->getVisibleOrigin();
|
1
2
3
4
5
6
|
bgHeight = (visibleSize.height + origin.y);
bgSprite1=CCSprite::create("background.png");
bgSprite1->setAnchorPoint(ccp(0.5, 0));
bgSprite1->setPosition(ccp(visibleSize.width/2 + origin.x,bgHeight));
this->addChild(bgSprite1, 0);
|
1
2
3
|
player = CCSprite::create("player.png");
player->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + player->getContentSize().height/2));
this->addChild(player,1);
|
这样,很简单我们就创建了一个背景精灵和一个飞机精灵,由于背景和飞机只有一个,所以我们可以考虑把他们设为类的成员
由于***和敌方飞机有很多,所以我们应该把他们单独写为函数,然后在init中设置一个计时器,通过计时器来设置多少秒调用一次函数,在函数中去添加***和敌方飞机精灵
首先先贴上添加***和敌方飞机的函数的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void GameScene::addMonster(){
CCSprite* pMonster = CCSprite::create("monster.png");
int minX = pMonster->getContentSize().width /2;
int maxX = visibleSize.width - pMonster->getContentSize().width /2;
int rangeX = maxX - minX;
int actualX = (rand() % rangeX) + minX;
pMonster->setPosition(ccp(actualX,origin.y + visibleSize.height - pMonster->getContentSize().height));
this->addChild(pMonster,1);
int time = (origin.y + visibleSize.height - pMonster->getContentSize().height)/monsterSpeed;
//time s内从原来位置到ccp(actualX,origin.y + pMonster->getContentSize().height/2)位置
CCMoveTo *actionMove = CCMoveTo::create(time,ccp(actualX,origin.y + pMonster->getContentSize().height/2));
CCCallFuncN *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(GameScene::spriteMoveFinished));
pMonster->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
|
1
2
3
4
5
6
7
8
9
|
void GameScene::addBullet1(){
CCSprite* pBullet = CCSprite::create("bullet1.png");
float x = player->getPositionX();
float y = player->getPositionY();
pBullet->setPosition(ccp(x,y+40));
this->addChild(pBullet,2);
CCMoveTo *actionMove = CCMoveTo::create(1,ccp(x,origin.y + visibleSize.height - pBullet->getContentSize().height));
CCCallFuncN *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(GameScene::spriteMoveFinished));
pBullet->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
|
第一段代码中首先我们先添加了一个敌方飞机的精灵,注意敌方飞机出现的位置的x值是随机的,所以我们应该把它设为他出现位置的最大x值和最小x值的随机数
然后我们开始设置他的移动,CCMoveTo::create函数中第一个参数是移动的时间,第二个参数是,从原来的点移动到的那个点的值,然后我们设置一个actionMoveDone来设置当敌方飞机移动完了之后就销毁它,销毁的代码我们就设置spriteMoveFinished中
1
2
3
4
|