cocos2d-x的学习模仿微信打飞机

时间:2023-02-06 19:26:30

最近有人用2d写了模仿微信的打飞机,我参考了下,用2dx来重新写下,我们一步步来,先整理好整个项目的框架,还有逻辑。我们先写一个有开始菜单的界面,

HelloWorldScene.h:

[cpp] view plaincopy
  1. class HelloWorld : public cocos2d::CCLayer  
  2. {  
  3. public:  
  4.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)  
  5.     virtual bool init();  
  6.   
  7.     // there's no 'id' in cpp, so we recommend to return the class instance pointer  
  8.     static cocos2d::CCScene* scene();  
  9.       
  10.     // a selector callback  
  11.     void menuCloseCallback(CCObject* pSender);  
  12.   
  13.     // preprocessor macro for "static create()" constructor ( node() deprecated )  
  14.     CREATE_FUNC(HelloWorld);  
  15.   
  16.     void initData();  
  17.       
  18.       
  19.     void startGame();  
  20. }  

HelloWorldScene.cpp:

[cpp] view plaincopy
  1. bool HelloWorld::init()  
  2. {  
  3.     //////////////////////////////  
  4.     // 1. super init first  
  5.     if ( !CCLayer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.   
  10.       
  11.     this->initData();  
  12.      
  13.     CCSprite *bgSprite = CCSprite::createWithSpriteFrameName("background_2.png");  
  14.     bgSprite->setPosition(ccp(wSize.width/2, wSize.height/2));  
  15.     this->addChild(bgSprite, 0);  
  16.       
  17.       
  18.     CCSprite *logoSprite = CCSprite::create("BurstAircraftLogo-hd.png");  
  19.     logoSprite->setPosition(ccp(wSize.width/2, wSize.height/2+100));  
  20.     this->addChild(logoSprite, 0);  
  21.       
  22.       
  23.       
  24.     CCMenuItemFont *startItem=CCMenuItemFont::create("开始游戏"this,menu_selector(HelloWorld::startGame));  
  25.       
  26.       
  27.     startItem->setPosition(ccp(wSize.width/2, wSize.height/2-200));  
  28.     startItem->setFontSizeObj(55);  
  29.     startItem->setFontNameObj("Georgia-Bold");  
  30.       
  31.     CCMenu *pMenu = CCMenu::create(startItem, NULL);  
  32.       
  33.     pMenu->setPosition(CCPointZero);  
  34.       
  35.     this->addChild(pMenu, 1);  
  36.   
  37.       
  38.       
  39.       
  40.     return true;  
  41. }  
  42.   
  43.   
  44. void HelloWorld::initData()  
  45. {  
  46.   
  47.   
  48.     CCSpriteFrameCache *frameCache=CCSpriteFrameCache::sharedSpriteFrameCache();  
  49.       
  50.     frameCache->addSpriteFramesWithFile("gameArts-hd.plist");  
  51.       
  52.   
  53.   
  54. }  
  55.   
  56.   
  57. //开始游戏  
  58. void HelloWorld::startGame()  
  59. {  
  60.   
  61.   
  62.     CCScene *scene=CCScene::create();  
  63.   
  64.     GameLayer *layer=GameLayer::create();  
  65.       
  66.     scene->addChild(layer);  
  67.       
  68.     //跳跃式动画  
  69.     CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.2f, scene));  
  70.   
  71.   
  72. }  

很简单的界面,效果如下:

cocos2d-x的学习模仿微信打飞机


我们看下游戏界面

GameScene.h:

[cpp] view plaincopy
  1. class GameLayer : public cocos2d::CCLayer  
  2. {  
  3.   
  4. public:  
  5.       
  6.     virtual bool init();  
  7.       
  8.     static cocos2d::CCScene* scene();  
  9.       
  10.       
  11.     CREATE_FUNC(GameLayer);  
  12.       
  13.   
  14. private:  
  15.     void initUI();  
  16.     void initData();  
  17.     void scrollBg();  
  18.     void update(float t);  
  19.       
  20.     cocos2d::CCSprite *bgSprite1;  
  21.     cocos2d::CCSprite *bgSprite2;  
  22.       
  23.       
  24.     int bgHeight;  
  25.   
  26.     cocos2d::CCSprite *playSprite;  
  27.       
  28.       
  29.   
  30. };  

GameScene.cpp

[cpp] view plaincopy
  1. bool GameLayer::init()  
  2. {  
  3.     if ( !CCLayer::init() )  
  4.     {  
  5.         return false;  
  6.     }  
  7.       
  8.       
  9.   
  10.   
  11.     this->initData();  
  12.       
  13.     this->initUI();  
  14.       
  15.     //系统的刷新  
  16.     this->scheduleUpdate();  
  17.       
  18.   
  19.     return true;  
  20.   
  21. }  
  22.   
  23. void GameLayer::initData()  
  24. {  
  25.   
  26.     //背景音乐  
  27.     SimpleAudioEngine::sharedEngine()->playBackgroundMusic("game_music.mp3",true);  
  28.       
  29.       
  30.     bgHeight=480;  
  31.   
  32. }  
  33.   
  34.   
  35. void GameLayer::initUI()  
  36. {  
  37.   
  38.     //背景  
  39.     bgSprite1=CCSprite::createWithSpriteFrameName("background_2.png");  
  40.     //锚点  
  41.     bgSprite1->setAnchorPoint(ccp(0.5, 0));  
  42.     bgSprite1->setPosition(ccp(320, 0));  
  43.       
  44.     this->addChild(bgSprite1, 0);  
  45.   
  46.       
  47.     bgSprite2=CCSprite::createWithSpriteFrameName("background_2.png");  
  48.     bgSprite2->setAnchorPoint(ccp(0.5, 0));  
  49.     bgSprite2->setPosition(ccp(320, bgHeight-1));  
  50.       
  51.     this->addChild(bgSprite2, 0);  
  52.   
  53.     //玩家  
  54.     playSprite=CCSprite::createWithSpriteFrameName("hero_fly_1.png");  
  55.     playSprite->setPosition(ccp(300, 200));  
  56.       
  57.     this->addChild(playSprite, 3);  
  58.   
  59.       
  60.     //自身动画  
  61.     CCArray *array=CCArray::create();  
  62.       
  63.     for (int i=1; i<3; i++)  
  64.     {  
  65.        
  66.         CCString *string=CCString::createWithFormat("hero_fly_%d.png",i);  
  67.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  68.           
  69.         array->addObject(frame);  
  70.           
  71.     }  
  72.       
  73.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  74.     CCAnimate *animate=CCAnimate::create(animation);  
  75.       
  76.     CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  77.       
  78.     playSprite->runAction(ac1);  
  79.       
  80.   
  81. }  
  82.   
  83. //循环滚动背景  
  84. void GameLayer::scrollBg()  
  85. {  
  86.   
  87.     bgHeight--;  
  88.       
  89.     if (bgHeight<=0)  
  90.     {  
  91.           
  92.           
  93.         bgHeight=480;  
  94.           
  95.           
  96.     }  
  97.   
  98.       
  99.     bgSprite1->setPosition(ccp(bgSprite1->getPosition().x, bgHeight-480));  
  100.     bgSprite2->setPosition(ccp(bgSprite2->getPosition().x, bgHeight-1));  
  101.   
  102.   
  103.   
  104. }  
  105.   
  106.   
  107. //刷新  
  108. void GameLayer::update(float t)  
  109. {  
  110.   
  111.   
  112.   
  113.     this->scrollBg();  
  114.   
  115.   
  116.   
  117.   
  118.   
  119. }  

cocos2d-x的学习模仿微信打飞机


在上面中,我们初始化了一些数据,精灵,设置滚动的背景,当然,我们需要一步步来实现,得想好逻辑,这个功能实现了,然后接下去该实现什么,然后直到完善。。。。


OK,我们继续。。。。。

接下来我们要让我们的飞机跟随我们的手指移动,简单点,我们就用layer的触摸事件来实现,设置setTouchEnabled为true就可以了,

在GameScene.cpp的initData函数中增加

[cpp] view plaincopy
  1. this->setTouchEnabled(true);  
并且在GameScene类的头文件中重写touch事件

[cpp] view plaincopy
  1. virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEven);  

在cpp中具体实现函数

[cpp] view plaincopy
  1. //手指移动飞机  
  2. void GameLayer::ccTouchesMoved(CCSet *pTouches,CCEvent *pEven)  
  3. {  
  4.   
  5.   
  6.     CCSetIterator it = pTouches->begin();  
  7.   
  8.     CCTouch* touch = (CCTouch*)(* it);  
  9.   
  10.   
  11.     CCPoint curLocation = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());  
  12.   
  13.   
  14.     //前一个点的坐标  
  15.     CCPoint oldLocation=touch->getPreviousLocation();  
  16.       
  17.     CCPoint translation=ccpSub(curLocation, oldLocation);  
  18.       
  19.       
  20.    // CCLOG("%f",translation.y);  
  21.     //相减,得到偏移量  
  22.     playSprite->setPosition(this->boundLayerPos(translation));  
  23.   
  24.   
  25.   
  26. }  
  27.   
  28.   
  29. CCPoint GameLayer::boundLayerPos(CCPoint newPos)  
  30. {  
  31.   
  32.     CCPoint point=newPos;  
  33.   
  34.     point.x=playSprite->getPosition().x+newPos.x;  
  35.     point.y=playSprite->getPosition().y+newPos.y;  
  36.   
  37.     if (point.x>=286*2) {  
  38.         point.x = 286*2;  
  39.     }else if (point.x<=33*2) {  
  40.         point.x = 33*2;  
  41.     }  
  42.       
  43.     if (point.y >=wSize.height-50*2) {  
  44.         point.y = wSize.height-50*2;  
  45.     }else if (point.y <= 43*2) {  
  46.         point.y = 43*2;  
  47.     }  
  48.   
  49.   
  50.   
  51.     return point;  
  52.   
  53. }  

有人说飞机位置直接设成移动的坐标点,如果不松手没关系,但一松手,再按住就出现问题了,

这样飞机就可以随着手指的移动而移动了。。。。。

然后我们增加发射子弹的功能,因为子弹是自动发射的,不需要通过发射按钮来发射子弹,所以这时候,你可以考虑就只创建一颗子弹精灵来实现,

我们在GameScene.cpp中增加新函数

[cpp] view plaincopy
  1. //制造子弹  
  2. void GameLayer::madeBullet()  
  3. {  
  4.   
  5.     bullet=CCSprite::createWithSpriteFrameName("bullet1.png");  
  6.   
  7.     this->addChild(bullet, 1);  
  8.       
  9.     //音效  
  10.    // SimpleAudioEngine::sharedEngine()->playEffect("bullet.mp3");  
  11.   
  12.       
  13.   
  14. }  
  15.   
  16. //重置子弹  
  17. void GameLayer::resetBullet()  
  18. {  
  19.   
  20.   
  21.     bulletSpeed = (wSize.height - (playSprite->getPosition().y + 50))/15;  
  22.   
  23.     if (bulletSpeed<5)  
  24.     {  
  25.         bulletSpeed=5;  
  26.     }  
  27.       
  28.     bullet->setPosition(ccp(playSprite->getPosition().x, playSprite->getPosition().y+50));  
  29.       
  30.   
  31.   
  32. }  
  33.   
  34.   
  35. //开火  
  36. void GameLayer::firingBullets()  
  37. {  
  38.   
  39.     bullet->setPosition(ccp(bullet->getPosition().x, bullet->getPosition().y+bulletSpeed));  
  40.       
  41.     if (bullet->getPosition().y > wSize.height - 20)  
  42.     {  
  43.           
  44.           
  45.         this->resetBullet();  
  46.           
  47.           
  48.           
  49.     }  
  50.   
  51.   
  52.   
  53.   
  54.   
  55. }  

在上面中,我们创建子弹,设置子弹的速度,开火,超出屏幕时,我们重置子弹的位置,

在update(float t)函数中,我们增加函数

[cpp] view plaincopy
  1. this->firingBullets();  

OK,这样就添加完了子弹:

cocos2d-x的学习模仿微信打飞机


OK,现在飞机可以移动,并且可以发射子弹,那么现在,我们需要加入敌人的飞机。。。。。。。

我们定义一个类继承CCSprite

上代码:

EnemyPlane.h

[cpp] view plaincopy
  1. //敌人的飞机  
  2. class EnemyPlane :public cocos2d::CCSprite  
  3. {  
  4.   
  5.       
  6.       
  7.       
  8. public:  
  9.       
  10.           
  11.     //飞机的种类  
  12.     int planeType;  
  13.       
  14.     //飞机的血量  
  15.     int hp;  
  16.       
  17.     //飞机的速度s  
  18.     int speed;  
  19.   
  20.     EnemyPlane();  
  21.       
  22.     ~EnemyPlane();  
  23.   
  24.   
  25.     static EnemyPlane* createWithSpriteFrameName(const char *spriteFrameName);  
  26.   
  27.   
  28. };  

EnemyPlane.cpp

[cpp] view plaincopy
  1. using namespace cocos2d;  
  2. using namespace CocosDenshion;  
  3.   
  4.   
  5. EnemyPlane::EnemyPlane()  
  6. {  
  7.   
  8.   
  9.   
  10. }  
  11.   
  12.   
  13.   
  14. EnemyPlane::~EnemyPlane()  
  15. {  
  16.   
  17.   
  18. }  
  19.   
  20.   
  21. EnemyPlane* EnemyPlane::createWithSpriteFrameName(const char *spriteFrameName)  
  22. {  
  23.   
  24.     EnemyPlane* pSprite = new EnemyPlane;  
  25.     if (pSprite && pSprite->initWithSpriteFrameName(spriteFrameName))  
  26.     {  
  27.         pSprite->autorelease();  
  28.         return pSprite;  
  29.     }  
  30.     CC_SAFE_DELETE(pSprite);  
  31.       
  32.     return NULL;  
  33.       
  34.       
  35.   
  36.   
  37.   
  38. }  

然后我们需要制造这个敌人的飞机,有各种类型的,

[cpp] view plaincopy
  1. //添加飞机  
  2. void GameLayer::addEnemyPlane()  
  3. {  
  4.   
  5.     smallPlaneTime++;  
  6.     mediumPlaneTime ++;  
  7.     bigPlaneTime ++;  
  8.       
  9.     if (smallPlaneTime > 60)  
  10.     {  
  11.   
  12.      
  13.         EnemyPlane *smallPlane=this->makeSmallPlane();  
  14.       
  15.         this->addChild(smallPlane, 3);  
  16.   
  17.         planeArray->addObject(smallPlane);  
  18.       
  19.           
  20.         smallPlaneTime = 0;  
  21.   
  22.       
  23.     }  
  24.       
  25.     if (mediumPlaneTime > 400)  
  26.     {  
  27.           
  28.           
  29.         EnemyPlane *mediumPlane=this->makeMediumPlane();  
  30.           
  31.         this->addChild(mediumPlane, 3);  
  32.           
  33.         planeArray->addObject(mediumPlane);  
  34.           
  35.           
  36.         mediumPlaneTime = 0;  
  37.           
  38.           
  39.     }  
  40.       
  41.     if (bigPlaneTime > 700)  
  42.     {  
  43.           
  44.           
  45.         EnemyPlane *bigPlane=this->makeBigPlane();  
  46.           
  47.         this->addChild(bigPlane, 3);  
  48.           
  49.         planeArray->addObject(bigPlane);  
  50.           
  51.           
  52.         bigPlaneTime = 0;  
  53.           
  54.           
  55.     }  
  56.   
  57.   
  58.   
  59. }  
  60.   
  61.   
  62. //制造小飞机  
  63. EnemyPlane* GameLayer::makeSmallPlane()  
  64. {  
  65.   
  66.   
  67.     EnemyPlane *smallPlane=EnemyPlane::createWithSpriteFrameName("enemy1_fly_1.png");  
  68.       
  69.     smallPlane->setPosition(ccp(arc4random()%290+17, 960));  
  70.       
  71.     smallPlane->planeType=1;  
  72.     smallPlane->hp=1;  
  73.     smallPlane->speed=arc4random()%4+2;  
  74.   
  75.     return smallPlane;  
  76.   
  77.   
  78. }  
  79.   
  80. //制造中等飞机  
  81. EnemyPlane* GameLayer::makeMediumPlane()  
  82. {  
  83.   
  84.     EnemyPlane *mediumPlane=EnemyPlane::createWithSpriteFrameName("enemy3_fly_1.png");  
  85.       
  86.     mediumPlane->setPosition(ccp(arc4random()%280+23, 960));  
  87.       
  88.     mediumPlane->planeType=3;  
  89.     mediumPlane->hp=15;  
  90.     mediumPlane->speed=arc4random()%3+2;  
  91.       
  92.     return mediumPlane;  
  93.   
  94.   
  95. }  
  96.   
  97.   
  98. //制造大飞机  
  99. EnemyPlane* GameLayer::makeBigPlane()  
  100. {  
  101.   
  102.       
  103.     //大飞机有浆的动画  
  104.     CCArray *array=CCArray::create();  
  105.       
  106.     for (int i=1; i<=2; i++)  
  107.     {  
  108.           
  109.         CCString *string=CCString::createWithFormat("enemy2_fly_%i.png",i);  
  110.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  111.           
  112.         array->addObject(frame);  
  113.           
  114.     }  
  115.       
  116.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  117.     CCAnimate *animate=CCAnimate::create(animation);  
  118.       
  119.     CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  120.       
  121.       
  122.       
  123.     EnemyPlane *bigPlane=EnemyPlane::createWithSpriteFrameName("enemy2_fly_1.png");  
  124.       
  125.     bigPlane->setPosition(ccp(arc4random()%210+55, 1200));  
  126.       
  127.     bigPlane->planeType=2;  
  128.     bigPlane->hp=25;  
  129.     bigPlane->speed=arc4random()%2+2;  
  130.       
  131.     bigPlane->runAction(ac1);  
  132.       
  133.       
  134.     array->removeAllObjects();  
  135.   
  136.       
  137.     return bigPlane;  
  138.   
  139.   
  140. }  
  141.   
  142.   
  143.   
  144. //移动飞机  
  145. void GameLayer::moveEnemyPlane()  
  146. {  
  147.   
  148.     for (int i=0;i<planeArray->count();i++)  
  149.     {  
  150.         EnemyPlane *tmpPlane=(EnemyPlane *)planeArray->objectAtIndex(i);  
  151.           
  152.         tmpPlane->setPosition(ccp(tmpPlane->getPosition().x,tmpPlane->getPosition().y - tmpPlane->speed));  
  153.           
  154.           
  155.         if (tmpPlane->getPosition().y<(-75*2))  
  156.         {  
  157.               
  158.             planeArray->removeObject(tmpPlane);  
  159.               
  160.             tmpPlane->removeFromParentAndCleanup(false);  
  161.               
  162.         }  
  163.           
  164.     }  
  165.   
  166.       
  167.   
  168. }  

在上面中,我们制作了不同类型的飞机,它们各有自己的特点,设置他们出现的时间,然后我们在update函数中增加

[cpp] view plaincopy
  1. this->addEnemyPlane();  
  2.       
  3.     this->moveEnemyPlane();  

OK,这样就有敌人的飞机啦。。。。。。。

cocos2d-x的学习模仿微信打飞机

。。。。。。。。。。。。未完。。。。

OK。。。。我们继续,接下来,我们要考虑碰撞检测了,我的飞机子弹打出来,让打中敌人的飞机,并且显示相应的分数。这里碰撞检测,我们要知道,什么跟什么碰撞了,这里很简单,飞机的子弹跟敌人的飞机有个碰撞,敌人的飞机跟我的飞机有个碰撞,目前是这么多,当然后面还有。上代码

在GameSence.h中

[cpp] view plaincopy
  1. void collisionDetection();  
  2.   
  3.    void enemyPlaneBlowupAnimation(EnemyPlane *enemyPlane);  
  4.      
  5.    void hitAnimationToFoePlane(EnemyPlane *enemyPlane);  
  6.      
  7.    void blowupEnd(CCObject *object);  
  8.      
  9.    //得分  
  10.    int scoreInt;  

GameSence.cpp

[cpp] view plaincopy
  1. //碰撞检测  
  2. void GameLayer::collisionDetection()  
  3. {  
  4.   
  5.     //子弹所在的矩形区域  
  6.     CCRect bulletRect=bullet->boundingBox();  
  7.   
  8.     //敌人飞机的数组  
  9.     // CCARRAY_FOREACH(<#__array__#>, <#__object__#>)  
  10.     for (int i=0; i<planeArray->count(); i++)  
  11.     {  
  12.         EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i);  
  13.           
  14.         //矩形和矩形  
  15.         if (bulletRect.intersectsRect(enemyPlane->boundingBox()))  
  16.         {  
  17.               
  18.             //碰到飞机了,子弹要消失,所以这里重置  
  19.             this->resetBullet();  
  20.               
  21.               
  22.             enemyPlane->hp=enemyPlane->hp-1;  
  23.               
  24.             if (enemyPlane->hp<=0)  
  25.             {  
  26.                   
  27.                 this->enemyPlaneBlowupAnimation(enemyPlane);  
  28.                 planeArray->removeObject(enemyPlane);  
  29.                    
  30.             }  
  31.             else  
  32.             {  
  33.               
  34.                 this->hitAnimationToFoePlane(enemyPlane);  
  35.               
  36.               
  37.             }  
  38.               
  39.         }  
  40.           
  41.           
  42.           
  43.     }  
  44.       
  45.   
  46.   
  47.   
  48.   
  49.   
  50. }  
  51.   
  52.   
  53. //敌人飞机爆炸效果  
  54. void GameLayer::enemyPlaneBlowupAnimation(EnemyPlane *enemyPlane)  
  55. {  
  56.   
  57.     int animationNum = 0;  
  58.       
  59.     //动画帧数跟相应的分数  
  60.     if (enemyPlane->planeType == 1)  
  61.     {  
  62.         animationNum = 4;  
  63.         scoreInt += 2000;  
  64.     }  
  65.       
  66.     if (enemyPlane->planeType == 3)  
  67.     {  
  68.         animationNum = 4;  
  69.         scoreInt += 10000;  
  70.     }  
  71.       
  72.     if (enemyPlane->planeType == 2)  
  73.     {  
  74.         animationNum = 7;  
  75.         scoreInt += 40000;  
  76.     }  
  77.   
  78.     CCString *string=CCString::createWithFormat("%i",scoreInt);  
  79.   
  80.     scoreLabel->setString(string->getCString());  
  81.   
  82.     //停止所以动画  
  83.     enemyPlane->stopAllActions();  
  84.   
  85.     //动画效果  
  86.     CCArray *array=CCArray::create();  
  87.       
  88.     for (int i=1; i<=animationNum; i++)  
  89.     {  
  90.           
  91.         CCString *string=CCString::createWithFormat("enemy%i_blowup_%i.png",enemyPlane->planeType,i);  
  92.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  93.           
  94.         array->addObject(frame);  
  95.           
  96.     }  
  97.       
  98.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  99.     CCAnimate *animate=CCAnimate::create(animation);  
  100.   
  101.   
  102.     CCSequence *seq=CCSequence::create(animate,CCCallFuncN::create(this, callfuncN_selector(GameLayer::blowupEnd)),NULL);  
  103.   
  104.   
  105.     enemyPlane->runAction(seq);  
  106.       
  107.     array->removeAllObjects();  
  108.       
  109.     if (enemyPlane->planeType == 3)  
  110.     {  
  111.   
  112.           
  113.       //  SimpleAudioEngine::sharedEngine()->playEffect("enemy1_down.mp3");  
  114.   
  115.           
  116.       
  117.     }  
  118.     else if (enemyPlane->planeType == 2)  
  119.     {  
  120.   
  121.      //   SimpleAudioEngine::sharedEngine()->playEffect("enemy2_down.mp3");  
  122.   
  123.       
  124.     }  
  125.     else if (enemyPlane->planeType == 1)  
  126.     {  
  127.   
  128.       
  129.     //    SimpleAudioEngine::sharedEngine()->playEffect("enemy3_down.mp3");  
  130.   
  131.       
  132.       
  133.     }  
  134.       
  135.       
  136.       
  137.   
  138. }  
  139.   
  140.   
  141. //动画结束后的处理  
  142. void GameLayer::blowupEnd(CCObject *object)  
  143. {  
  144.   
  145.   
  146.     EnemyPlane *enemyPlane=(EnemyPlane *)object;  
  147.   
  148.     enemyPlane->removeFromParentAndCleanup(false);  
  149.   
  150.   
  151. }  
  152.   
  153.   
  154. //飞机被击打的动画  
  155. void GameLayer::hitAnimationToFoePlane(EnemyPlane *enemyPlane)  
  156. {  
  157.   
  158.     //中等飞机  
  159.     if (enemyPlane->planeType==3)  
  160.     {  
  161.           
  162.         if (enemyPlane->hp==13)  
  163.         {  
  164.               
  165.               
  166.             CCArray *array=CCArray::create();  
  167.               
  168.             for (int i=1; i<=2; i++)  
  169.             {  
  170.                   
  171.                 CCString *string=CCString::createWithFormat("enemy3_hit_%i.png",i);  
  172.                 CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  173.                   
  174.                 array->addObject(frame);  
  175.                   
  176.             }  
  177.               
  178.             CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  179.             CCAnimate *animate=CCAnimate::create(animation);  
  180.               
  181.             CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  182.   
  183.             enemyPlane->runAction(ac1);  
  184.               
  185.             array->removeAllObjects();  
  186.               
  187.         }  
  188.           
  189.           
  190.           
  191.           
  192.     }  
  193.     //大飞机  
  194.     else if (enemyPlane->planeType==2)  
  195.     {  
  196.       
  197.         if (enemyPlane->hp==20)  
  198.         {  
  199.             CCArray *array=CCArray::create();  
  200.               
  201.             for (int i=1; i<=1; i++)  
  202.             {  
  203.                   
  204.                 CCString *string=CCString::createWithFormat("enemy2_hit_%i.png",i);  
  205.                 CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  206.                   
  207.                 array->addObject(frame);  
  208.                   
  209.             }  
  210.               
  211.             CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  212.             CCAnimate *animate=CCAnimate::create(animation);  
  213.               
  214.             CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  215.               
  216.             enemyPlane->runAction(ac1);  
  217.               
  218.             array->removeAllObjects();  
  219.   
  220.               
  221.         }  
  222.       
  223.           
  224.     }  
  225.   
  226.   
  227. }  

上面中,我们定义了一个碰撞检测的函数collisionDetection,通过intersectsRect函数来判断子弹的矩形区域是否在敌人飞机的矩形区域内,打一下少一滴血,当血小于等于0时,就触发我们的飞机爆炸效果,并且移除死亡的飞机,设置分数显示,否则打击的时候,有打击动画。大体的逻辑就是这样。。。。。

cocos2d-x的学习模仿微信打飞机


在这篇文章中,我们将把剩下的功能完成,首先,我们加入换子弹或是炸弹的功能,它的原理大体是这样的,设置物品出现的时间间隔,会随机出现物品的种类,然后判断物品跟飞机所在区域,碰撞检测,在一起就算是吃到这个物品了,。。OK,下面上代码

首页新建一个类继承CCNode,

ChangeBullet.h

[cpp] view plaincopy
  1. typedef enum{  
  2.   
  3.     propsTypeBomb = 4,  
  4.     propsTypeBullet = 5  
  5.   
  6.   
  7. }prosType;  
  8.   
  9. class ChangeBullet:public cocos2d::CCNode  
  10. {  
  11.   
  12.   
  13.   
  14. public:  
  15.       
  16.     cocos2d::CCSprite *prop;  
  17.       
  18.       
  19.     prosType bulletType;  
  20.       
  21.     void initWithType(prosType type);  
  22.       
  23.     void propAnimation();  
  24.       
  25.   
  26.     static ChangeBullet* create(void);  
  27.       
  28.     CC_SYNTHESIZE_RETAIN(cocos2d::CCSprite*, _prop, Prop);  
  29.       
  30.     ChangeBullet();  
  31.     ~ChangeBullet();  
  32.   
  33. };  
ChangeBullet.cpp

[cpp] view plaincopy
  1. ChangeBullet::~ChangeBullet()  
  2. {  
  3.   
  4.   
  5.   
  6.   
  7. }  
  8.   
  9. ChangeBullet::ChangeBullet()  
  10. {  
  11.   
  12.   
  13.   
  14.   
  15. }  
  16.   
  17. ChangeBullet* ChangeBullet::create()  
  18. {  
  19.   
  20.     ChangeBullet * pRet = new ChangeBullet();  
  21.     if (pRet && pRet->init())  
  22.     {  
  23.         pRet->autorelease();  
  24.     }  
  25.     else  
  26.     {  
  27.         CC_SAFE_DELETE(pRet);  
  28.     }  
  29.     return pRet;  
  30.   
  31. }  
  32.   
  33. void ChangeBullet::initWithType(prosType type)  
  34. {  
  35.   
  36.   
  37.     bulletType=type;  
  38.       
  39.       
  40.     CCString *string=CCString::createWithFormat("enemy%i_fly_1.png",type);  
  41.   
  42.   
  43.     prop=CCSprite::createWithSpriteFrameName(string->getCString());  
  44.       
  45.     prop->setPosition(ccp((arc4random()%268 + 23)*2,632*2));  
  46.       
  47.   
  48. }  
  49.   
  50.   
  51. //物品出现动画  
  52. void ChangeBullet::propAnimation()  
  53. {  
  54.   
  55.   
  56.     CCMoveTo *ac1=CCMoveTo::create(1, ccp(prop->getPosition().x, 250*2));  
  57.     CCMoveTo *ac2=CCMoveTo::create(0.4, ccp(prop->getPosition().x, 252*2));  
  58.     CCMoveTo *ac3=CCMoveTo::create(1, ccp(prop->getPosition().x, 632*2));  
  59.     CCMoveTo *ac4=CCMoveTo::create(2, ccp(prop->getPosition().x, -55*2));  
  60.   
  61.   
  62.   
  63.     prop->runAction(CCSequence::create(ac1,ac2,ac3,ac4,NULL));  
  64.   
  65.   
  66. }  

在上面中,我们定义了一个换物品的类,设置它的物品种类,威力更强的子弹和炸弹,并且构造一个物品出现动画的函数,这个动画函数很简单,都是CCMove构成。。。

GameScene.h

[cpp] view plaincopy
  1. void addBulletTypeTip();  
  2.      
  3.    //空降物品时间计数  
  4.    int propTime;  
  5.      
  6.    ChangeBullet *prop;  
  7.      
  8.    bool isVisible;  
  9.    bool isBigBullet;  
  10.    bool isChangeBullet;  
  11.      
  12.    //子弹持续时间  
  13.    int bulletlastTime;  
  14.      
  15.    void bulletLastTime();  

GameScene.cpp

[cpp] view plaincopy
  1. //空降物  
  2. void GameLayer::addBulletTypeTip()  
  3. {  
  4.   
  5.     propTime++;  
  6.       
  7.     if (propTime>500)  
  8.     {  
  9.           
  10.         prop=ChangeBullet::create();  
  11.           
  12.         prop->initWithType((prosType)(arc4random()%2 + 4));  
  13.           
  14.         this->addChild(prop->prop);  
  15.           
  16.         prop->propAnimation();  
  17.           
  18.         prop->retain();  
  19.           
  20.         propTime=0;  
  21.           
  22.         isVisible=true;  
  23.           
  24.           
  25.     }  
  26.   
  27.   
  28.   
  29. }  
  30.   
  31.   
  32. void GameLayer::bulletLastTime()  
  33. {  
  34.   
  35.     if (isBigBullet)  
  36.     {  
  37.         if (bulletlastTime > 0)  
  38.         {  
  39.             bulletlastTime --;  
  40.         }  
  41.         else  
  42.         {  
  43.             bulletlastTime = 1200;  
  44.             isBigBullet = false;  
  45.             isChangeBullet = true;  
  46.         }  
  47.     }  
  48.   
  49.   
  50.   
  51. }  

然后在我们的update函数中

[cpp] view plaincopy
  1. this->addBulletTypeTip();  
  2.      
  3.      
  4.    this->bulletLastTime();  


在collisionDetection函数中

[cpp] view plaincopy
  1. //飞机跟空降物  
  2.     if (isVisible==true)  
  3.     {  
  4.         CCRect playRect=playSprite->boundingBox();  
  5.         CCRect propRect=prop->prop->boundingBox();  
  6.           
  7.           
  8.         if (playRect.intersectsRect(propRect))  
  9.         {  
  10.               
  11.             prop->prop->stopAllActions();  
  12.             prop->prop->removeFromParentAndCleanup(true);  
  13.               
  14.             isVisible=false;  
  15.               
  16.             //换子弹  
  17.             if (prop->bulletType==propsTypeBullet)  
  18.             {  
  19.                // CCLOG("--------22222");  
  20.                   
  21.                 isBigBullet = true;  
  22.                 isChangeBullet = true;  
  23.                   
  24.             }  
  25.             //炸弹  
  26.             else  
  27.             {  
  28.               
  29.                 for (int i=0; i<planeArray->count(); i++)  
  30.                 {  
  31.                      
  32.                     EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i);  
  33.   
  34.                     //爆炸动画  
  35.                     this->enemyPlaneBlowupAnimation(enemyPlane);  
  36.                       
  37.                       
  38.                 }  
  39.                   
  40.                 planeArray->removeAllObjects();  
  41.               
  42.               
  43.             }  
  44.               
  45.               
  46.               
  47.         }  
  48.           
  49.     }  


OK。。。看下效果图

cocos2d-x的学习模仿微信打飞机 cocos2d-x的学习模仿微信打飞机

OK,到现在我们还没做自己飞机的碰撞,那么接下来我们在collisionDetection函数中,再加入一个判断,判断敌人的飞机跟我们自己的飞机是否碰撞了,上代码

[cpp] view plaincopy
  1. CCRect playRec=playSprite->boundingBox();  
  2.     playRec.origin.x += 25*2;  
  3.     playRec.size.width -= 50*2;  
  4.     playRec.origin.y -= 10*2;  
  5.     playRec.size.height -= 10*2;  
  6.       
  7.     for (int i=0; i<planeArray->count(); i++)  
  8.     {  
  9.           
  10.         EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i);  
  11.   
  12.           
  13.         if (playRec.intersectsRect(enemyPlane->boundingBox()))  
  14.         {  
  15.               
  16.               
  17.             this->playerBlowupAnimation();  
  18.             this->enemyPlaneBlowupAnimation(enemyPlane);  
  19.             this->gameOver();  
  20.   
  21.             planeArray->removeObject(enemyPlane);  
  22.               
  23.               
  24.               
  25.         }  
  26.           
  27.           
  28.     }  

[cpp] view plaincopy
  1. //自己的飞机爆炸动画  
  2. void GameLayer::playerBlowupAnimation()  
  3. {  
  4.     playSprite->stopAllActions();  
  5.       
  6.       
  7.       
  8.     CCArray *array=CCArray::create();  
  9.       
  10.     for (int i=1; i<=4; i++)  
  11.     {  
  12.           
  13.         CCString *string=CCString::createWithFormat("hero_blowup_%i.png",i);  
  14.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  15.           
  16.         array->addObject(frame);  
  17.           
  18.     }  
  19.       
  20.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);  
  21.     CCAnimate *animate=CCAnimate::create(animation);  
  22.       
  23.     CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  24.       
  25.     playSprite->runAction(ac1);  
  26.       
  27.     array->removeAllObjects();  
  28.   
  29.   
  30.   
  31.   
  32. }  

上面中,我们做了碰撞检测,一旦飞机跟敌人的飞机撞上,我们就认为游戏结束了,所以最后我们加上个游戏结束的画面

[cpp] view plaincopy
  1. //游戏结束  
  2. void GameLayer::gameOver()  
  3. {  
  4.   
  5.   
  6.     isGameOver=true;  
  7.       
  8.       
  9.     this->gamePause();  
  10.   
  11.   
  12.   
  13.     CCLayerColor *gameoverLayer=CCLayerColor::create(ccc4(215, 221, 222, 255), 582, 544);  
  14.       
  15.       
  16.     gameoverLayer->setPosition(ccp(wSize.width/2-582/2, 220));  
  17.   
  18.     this->addChild(gameoverLayer, 3);  
  19.   
  20.       
  21.     CCLabelTTF *ttfLabel=CCLabelTTF::create("飞机大战分数""MarkerFelt-Thin", 50);  
  22.     ttfLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-ttfLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-70));  
  23.     ttfLabel->setColor(ccc3(0, 0, 0));  
  24.     ttfLabel->setAnchorPoint(ccp(0, 0));  
  25.     gameoverLayer->addChild(ttfLabel, 1);  
  26.       
  27.       
  28.       
  29.     CCLabelTTF *scLabel=CCLabelTTF::create(scoreLabel->getString(), "MarkerFelt-Thin", 44);  
  30.     scLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-scLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-250));  
  31.     scLabel->setColor(ccc3(0, 0, 0));  
  32.     scLabel->setAnchorPoint(ccp(0, 0));  
  33.     gameoverLayer->addChild(scLabel, 1);  
  34.       
  35.       
  36.       
  37.     CCMenuItemFont *startItem=CCMenuItemFont::create("继续"this,menu_selector(GameLayer::restart));  
  38.   
  39.       
  40.     startItem->setPosition(ccp(gameoverLayer->getContentSize().width/2, 60));  
  41.     startItem->setFontSizeObj(50);  
  42.     startItem->setFontNameObj("Georgia-Bold");  
  43.     startItem->setColor(ccc3(0, 0, 0));  
  44.       
  45.     CCMenu *pMenu = CCMenu::create(startItem, NULL);  
  46.       
  47.     pMenu->setPosition(CCPointZero);  
  48.       
  49.     gameoverLayer->addChild(pMenu, 1);  
  50.       
  51.   
  52.       
  53.       
  54. }  
  55.   
  56.   
  57. //游戏暂停  
  58. void GameLayer::gamePause()  
  59. {  
  60.   
  61.     if (isGameOver==false)  
  62.     {  
  63.           
  64.           
  65.           
  66.           
  67.     }  
  68.     else  
  69.     {  
  70.       
  71.       
  72.         CCObject *object;  
  73.           
  74.         CCARRAY_FOREACH(this->getChildren(), object)  
  75.         {  
  76.               
  77.             CCNode *node=(CCNode *)object;  
  78.           
  79.             node->stopAllActions();  
  80.           
  81.         }  
  82.   
  83.           
  84.           
  85.       
  86.       
  87.     }  
  88.   
  89.   
  90.   
  91.   
  92.   
  93.   
  94. }  

cocos2d-x的学习模仿微信打飞机


OK.。。。到这里,基本上大体的功能有了,呵呵,貌似暂停还木有。。。。

下篇文章中,我们将来探究一下在ios平台上适配的问题,当然还有android等其他平台的适配,。。。。。。。。。