cocos2dx实现经典飞机大战

cocos2dx实现经典飞机大战,第1张

概述游戏开始层 #ifndef __LayerGameStart_H__#define __LayerGameStart_H__#include "cocos2d.h"USING_NS_CC;class LayerGameStart :public CCLayer{public: static CCScene * scene(); CREATE_FUNC(LayerGa 游戏开始层
#ifndef __LayerGameStart_H__#define __LayerGameStart_H__#include "cocos2d.h"USING_NS_CC;class LayerGameStart :public cclayer{public:    static CCScene * scene();    CREATE_FUNC(LayerGameStart);    bool init();    voID addStartGamePicture();    voID addPreLoadMusic();    voID toMainGameCallback();};#endif
游戏主场景
#include "cocos2d.h"#include "Plane.h"#include "LayerGameStart.h"#include "LayerBullet.h"#include "LayerEnemy.h"#include "LayerControl.h"#include "LayerFood.h"#include "LayerGameOver.h"USING_NS_CC;const int  SMALL_score = 1000;const int  MID_score = 6000;const int  BIG_score = 30000;const int MAX_BIGBOOM_COUNT = 100000;const int TAG_BIGBOOM = 1000;const int TAG_BIGBOOMCOUNT = 2000;class LayerGameMain :public cclayer{public:    static CCScene * scene();    CREATE_FUNC(LayerGameMain);    bool init();    //加载主场景背景    voID addBackGround();    //背景动起来    voID movingBackGround(float dt);    voID addHero();    voID addBulletLayer();    voID addEnemyLayer();    voID addCtrlLayer();    voID addFoodLayer();    voID updateBigBoomCount(int bigBoomCount);    voID boomMenuCallBack(CCObject * obj);    enum BACKGROUND    {        BACK1,BACK2    };    voID update(float dt);    virtual bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);    virtual voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent);    LayerBullet  * _bulletLayer;    LayerEnemy   * _enemyLayer;    LayerControl * _ctrlLayer;    LayerFood    * _foodLayer;    static int score;    static voID setscore(int num);    int bigBoomCount;};
碰撞检测

其中分为子d与飞机的碰撞检测,飞机与飞机的碰撞检测,其中敌机分为大飞机,中飞机与小飞机,原理大致相同,这里只展示小飞机的原理与方法。

CCARRAY_FOREACH(_bulletLayer->_bulletArray,bt)    {        CCSprite * bullet = (CCSprite*)bt;        CCArray  * smallEnemyToDel = CCArray::create();

“`

CCARRAY_FOREACH(_enemyLayer->smallArray,et) { Enemy * smallEnemy = (Enemy *)et; if (bullet->boundingBox().intersectsRect(smallEnemy->getBoundingBox())) { cclog("%d",smallEnemy->getlife()); if (smallEnemy->getlife() == 1) { smallEnemy->loselife(); bulletsToDel->addobject(bullet); smallEnemyToDel->addobject(smallEnemy); score += SMALL_score; _ctrlLayer->updatascore(score); } } } CCARRAY_FOREACH(smallEnemyToDel,et) { Enemy * smallEnemy = (Enemy*)et; _enemyLayer->smallEnemyBlowUp(smallEnemy); } smallEnemyToDel->release();}
飞机与飞机的碰撞检测
//hero vs enemySmallCCRect planeRect = Plane::getInstance()->boundingBox();planeRect.origin.x += 30;planeRect.origin.y += 20;planeRect.size.wIDth -= 60;planeRect.size.height -= 40;CCARRAY_FOREACH(_enemyLayer->smallArray,et){    Enemy  * smallEnemy = (Enemy  *)et;    if (planeRect.intersectsRect(smallEnemy->getBoundingBox()))    {        _bulletLayer->stopShoot();        unscheduleAllSelectors();        Plane::getInstance()->blowUp();        Plane::getInstance()->removePlane();        _enemyLayer->smallEnemyBlowUp(smallEnemy);    }}
飞机大战的分层

由于游戏的复杂性,需要将精灵分为好几个层次加入到游戏中,可以分为控制层,敌机层,食物层,子d层等

控制层代码
#ifndef __LayerControl_H__    #define __LayerControl_H__    #include "cocos2d.h"    USING_NS_CC;    class LayerControl : public cclayer    {    public:        CREATE_FUNC(LayerControl);        bool init();        voID menuCallBack(CCObject *obj);        voID updatascore(int score);    private:        CcmenuItemSprite * pauseMenuItem;        cclabelBMFont * scoreItem;    };    #endif
食物层代码
#ifndef __LayerFood_H__#define __LayerFood_H__#include "cocos2d.h"USING_NS_CC;class LayerFood :public cclayer{public:    CREATE_FUNC(LayerFood);    bool init();    //voID addMultiBullets(float dt);    //voID multiBulletsMoveFinished(CCNode * pSender);    //voID removeMultiBullets(CCSprite *mb);    voID addBigBoom(float dt);    voID bigBoomMoveFinished(CCNode *pSender);    voID removeBigBoom(CCSprite *bb);public:    CCArray * multiBulletArray;    CCArray * bigBoomArray;};#endif
敌机层代码
#ifndef __LayerEnemy_H__#define __LayerEnemy_H__#include "cocos2d.h"#include "Enemy.h"USING_NS_CC;const int SMALL_MAXliFE = 1;const int MID_MAXliFE = 3;const int BIG_MAXliFE = 5;class LayerEnemy: public cclayer{public:    CREATE_FUNC(LayerEnemy);    bool init();    voID addSmallEnemy(float dt);    voID smallEnemyMovefinished(CCNode *node);    voID smallEnemyBlowUp(Enemy * smallEnemy);    voID removeSmallEnemy(CCNode * target,voID * data);    voID removeAllSmallEnemy();    voID addMIDEnemy(float dt);    voID mIDHitAnimate(Enemy *mIDEnemy);    voID mIDEnemyMovefinished(CCNode *node);    voID mIDEnemyBlowUp(Enemy * mIDEnemy);    voID removeMIDEnemy(CCNode * target,voID * data);    voID removeAllMIDEnemy();    voID addBigEnemy(float dt);    voID bigHitAnimate(Enemy *mIDEnemy);    voID bigEnemyMovefinished(CCNode *node);    voID bigEnemyBlowUp(Enemy * bigEnemy);    voID removeBigEnemy(CCNode * target,voID * data);    voID removeAllBigEnemy();    voID removeAllEnemy();    CCArray * smallArray;    CCArray *mIDArray;    CCArray *bigArray;};#endif
子d层代码
#ifndef __LayerBullet_H__#define __LayerBullet_H__#include "cocos2d.h"USING_NS_CC;class LayerBullet: public cclayer{public:    CREATE_FUNC(LayerBullet);    bool init();    voID startShoot();    voID stopShoot();    voID addBulletCallback(float dt);    voID bulletMoveFinished(CCNode* node);    voID removeBullet(CCSprite* bullet);    CCArray *_bulletArray;    CCSpriteBatchNode * _bulletBatchNode;};#endif
运行结果


总结

以上是内存溢出为你收集整理的cocos2dx实现经典飞机大战全部内容,希望文章能够帮你解决cocos2dx实现经典飞机大战所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存