坦克大战实现

坦克大战实现,第1张

概述先将常用的函数与头文件,宏定义等写到一个公共类中 #ifndef __Common_H__ #define __Common_H__ #include "cocos2d.h" #include "SimpleAudioEngine.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; 先将常用的函数与头文件,宏定义等写到一个公共类中
#ifndef __Common_H__    #define __Common_H__    #include "cocos2d.h"    #include "SimpleAudioEngine.h"    #include "cocos-ext.h"    USING_NS_CC;    USING_NS_CC_EXT;    #define winSize CCDirector::sharedDirector()->getWinSize()    #define TANK_SPEED 2    #define BulLET_SPEED 4    #define TANK_CREATE_INTERVAL 3    #define TANK_SHOOT_INTERVAL 1    #define TIME_OVER 300    #define TANK_BOMB "TANK_BOMB"    class Common    {    public:        enum DIRECTION        {            UP,RIGHT,DOWN,left,INVALID_DIR        };        enum TileType        {            NONE,BLOCK,STEEL,HOME        };        enum         {            ZO_LAYER_0=100,ZO_BulLET,ZO_TANK,ZO_LAYER_1,ZO_ITEM,ZO_LAYER_SETUP        };        static TileType getTileType(int gID)        {            if (gID==5||gID==6||gID==11||gID==12)            {                return STEEL;            }            if (gID==13||gID==14||gID==19||gID==20)            {                return BLOCK;            }            if (gID==17||gID==18||gID==23||gID==24)            {                return HOME;            }             return NONE;        }        static CCScene* scene(cclayer *layer)        {            CCScene *s=CCScene::create();            s->addChild(layer);            return s;        }        static const char* format(int v,const char *prefix="",const char *suffix="")        {            static char buf[1024];            sprintf(buf,"%s%d%s",prefix,v,suffix);            return buf;        }        static voID moveNodeX(CCNode *n,int dx)        {            n->setpositionX(n->getpositionX()+dx);        }        static voID moveNodeY(CCNode *n,int dy)        {            n->setpositionY(n->getpositionY()+dy);        }        static voID moveNode(CCNode*n,CCPoint pt)        {            moveNodeX(n,pt.x);            moveNodeY(n,pt.y);        }        static const CCPoint Map2PointLB(CCTMXTiledMap *_map,CCPoint ptTile)        {            ptTile.y=_map->getMapSize().height-1-ptTile.y;            return ccp(ptTile.x*_map->getTileSize().wIDth,ptTile.y*_map->getTileSize().height);        }        static CCPoint Point2Tile(CCTMXTiledMap *_map,CCPoint ptMap)        {            int dx=_map->getTileSize().wIDth;            int dy=_map->getTileSize().height;            //CCPoint ptMap=_map->convertToNodeSpace(ptGL);            int x=ptMap.x/dx;            int y=ptMap.y/dy;            y=_map->getMapSize().height-1-y;            return ccp(x,y);        }    };    #endif
开始界面与选择界面非常简单,可以略过,游戏主界面包含的方法如下
#ifndef __LayerGame_H__    #define __LayerGame_H__    #include "Common.h"    #include "TankFrIEnd.h"    #include "MenuCtrl.h"    #include "AI.h"    #include "LayerSetup.h"    class Item;    class LayerGame:public cclayer    {    public:        //CREATE_FUNC(LayerGame);        voID onExit();        static LayerGame*create(unsigned int index)        {            LayerGame *pRt=new LayerGame();            if (pRt&&pRt->init(index))            {                pRt->autorelease();                return pRt;            }else            {                delete pRt;                pRt=NulL;                return pRt;            }        }        bool init(unsigned int index);        voID update(float);        CCTMXTiledMap *_map;        TankFrIEnd *_tankFrIEnd;        voID DirCtrlPress(CCObject*);        voID Shoot(CCObject*);        voID Setup(CCObject*);        voID TimeOut(float);        voID setGodMode(int interval)        {            _godMode=true;            CCDelayTime*delay=CCDelayTime::create(interval);            CCCallFunc *callFunc=CCCallFunc::create(this,callfunc_selector(LayerGame::unSetGodMode));            this->runAction(CCSequence::createWithTwoActions(delay,callFunc));        }        voID unSetGodMode()        {            _godMode=false;        }        voID addItem();        voID eatItem(Item*);        voID eatItemBomb();        voID eatItemSteel();        voID setPause()        {            CCDirector::sharedDirector()->pause();        }        enum FAILURE_REASON        {            TIME_OUT,HOME_DESTORY,TANK_DIE        };        static voID gameOver(FAILURE_REASON reason);        CCArray *_bullets;        CCArray *_items;        AI *_ai;        int _victoryCount;        int _curCount;        int _index;        int _life;        bool _godMode;    };    #endif
添加友军坦克
@H_301_288@ class TankFrIEnd:public CCSprite { public: CREATE_FUNC(TankFrIEnd); bool init(); CCTMXTiledMap *getMap(); voID setTileSize(int tileSize); int _tileSize; voID Move(Common::DIRECTION dir); voID doMove(); voID Turn(Common::DIRECTION dir); bool canMove(Common::DIRECTION dir); Bullet *Shoot(); Common::DIRECTION _dir; int _damage; };
添加控制按钮,控制坦克的前进,后退与发射

其中前进,后退是按着一直走,发射子d是按一下,发射一下

bool MenuCtrl::init()    {        Ccmenu::init();        schedule(schedule_selector(MenuCtrl::CheckActive));        return true;    }    voID MenuCtrl::CheckActive(float)    {        if (this->m_pSelectedItem&&m_eState==kCcmenuStateTrackingtouch)        {            m_pSelectedItem->activate();        }    }
以上重写了menu方法,使得可以按着按钮一直运动 添加子d层
class Bullet:public CCSprite    {    public:        CREATE_FUNC(Bullet);        bool init();        voID setPicture(const char *filename);        static voID CheckBullets(CCTMXTiledMap *_map,CCArray *_bullets);        int _damage;    };
敌军坦克由电脑随机生成 敌军坦克类继承友军坦克
bool TankEnemy::init(){    CCSprite::initWithfile("tank/armor/armor2U.png");    _tileSize=0;    _dir=Common::UP;    this->setZOrder(Common::ZO_TANK);    Turn(Common::DOWN);    return true;}
随机产生敌军坦克方法
#ifndef __AI_H__    #define __AI_H__    #include "Common.h"    class AI:public CCNode    {    public:        CREATE_FUNC(AI);        bool init();        voID onExit();        CCTMXTiledMap *getMap()        {            return (CCTMXTiledMaP*)getParent();        }        //generate enemy tank        voID createTank(float);        bool _createFlag;        CCArray *_tanks;        CCArray *_bullets;        //move enemy tank        voID moveTank(float);        //make enemy tank shoot bullet        voID Shoot(float);        //collision detection        voID update(float);        bool _pause;        voID pause();        voID resume();    };    #endif
添加 多种道具,当打死一坦克时就会有一定概率产生一个道具@H_240_502@
class Item:public CCSprite    {    public:        enum ItemType        {            IT_ACTIVE,IT_BOMB,IT_MINTANK,IT_STAR,IT_STEEL,IT_TIMER,IT_COUNT        };        CREATE_FUNC(Item);        bool init();        voID setposition();        ItemType _type;    };
添加控制按钮,产生模态对话框,控制音量与音效
#ifndef __LayerSetup_H__    #define __LayerSetup_H__    #include "Common.h"@H_301_288@ class MyCCControlSlIDer : public CCControlSlIDer { public: static MyCCControlSlIDer* create(const char* bgfile,const char* progressfile,const char* thumbfile) { // Prepare background for slIDer CCSprite *backgroundSprite = CCSprite::create(bgfile); // Prepare progress for slIDer CCSprite *progresssprite = CCSprite::create(progressfile); // Prepare thumb (menuItem) for slIDer CCSprite *thumbSprite = CCSprite::create(thumbfile); MyCCControlSlIDer *pRet = new MyCCControlSlIDer(); pRet->initWithSprites(backgroundSprite,progresssprite,thumbSprite); pRet->autorelease(); return pRet; } bool cctouchBegan(CCtouch*t,CCEvent*e) { return CCControlSlIDer::cctouchBegan(t,e); } voID cctouchended(CCtouch*t,CCEvent*e) { CCControlSlIDer::cctouchended(t,e); } voID cctouchmoved(CCtouch*t,CCEvent*e) { CCControlSlIDer::cctouchmoved(t,e); } voID cctouchCancelled(CCtouch*t,CCEvent*e) { CCControlSlIDer::cctouchCancelled(t,e); } }; class LayerSetup:public cclayer { public: CREATE_FUNC(LayerSetup); bool init(); voID ChangeBGVolume(CCObject*,CCControlEvent); voID ChangeEffectVolume(CCObject*,CCControlEvent); voID Close(CCObject*); bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent); voID cctouchended(CCtouch *ptouch,CCEvent *pEvent); voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent); voID cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent); Ccmenu *_menu; bool _bMenuItemClicked; MyCCControlSlIDer* _volumeBG; bool _bVolumeBGClicked; MyCCControlSlIDer* _volumeEffect; bool _bVolumeEffectClicked; }; #endif // !--

由于CCControlSlIDer中有些成员变量是保护的,需要将CCControlSlIDer重写,才可以进行。

注意:当在实现暂停道具的时候,由于要暂停与重启,重启时报错
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention

原先用//scheduleOnce(schedule_selector(AI::resume),1);来调用resume函数,老是报错,帧循环发生错误,后来改成
CCDelayTime* delay=CCDelayTime::create(5.0f);
CCCallFunc* callFunc = CCCallFunc::create(this,callfunc_selector(AI::resume));
this->runAction(CCSequence::create(delay,callFunc,NulL));

就成功了,原因暂且不明。

运行结果 总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存