溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

cocos2d-x 2x 菜鳥編塔防 01 準(zhǔn)備工作

發(fā)布時(shí)間:2020-06-29 07:05:21 來(lái)源:網(wǎng)絡(luò) 閱讀:2651 作者:83628410 欄目:游戲開發(fā)
一.前言
最近在學(xué)習(xí)2dx,看了幾個(gè)源碼,手很是癢癢,下面跟我一下編寫一個(gè)類似保衛(wèi)大蘿卜的塔防游戲吧。


二.不廢話開始編代碼。
我用的cocos2d-x 的版本是2.04 ,編輯器是vs2010.首先創(chuàng)建一個(gè)工程,我們?nèi)∶纸?ldquo;TDgame”。
之后我們添加幾個(gè)常用到的模塊。
 
1.動(dòng)畫管理器,AnimationManager類

 AnimationManager類這是個(gè)單例的類,大家要問(wèn)這樣弄個(gè)單例類出來(lái)有什么好處呢,有什么作用呢,一開始我也是不明白,最后看了看魔塔的源代碼,總結(jié)了一下:
  1. 實(shí)現(xiàn)了一體化,也就是說(shuō)把所有的動(dòng)畫加載都放到一個(gè)函數(shù)中,我們可以讓這個(gè)函數(shù)出現(xiàn)在想要的位置上,比如,我們有一大堆的精靈動(dòng)畫,如果在游戲中加載起來(lái)會(huì)讓游戲有點(diǎn)卡,這時(shí)候我們就可以創(chuàng)建一個(gè)進(jìn)入游戲時(shí)的加載頁(yè)面,同時(shí)執(zhí)行加載動(dòng)畫。一般游戲也都是這么做的。
  2. 規(guī)范代碼,有利于編寫代碼。

好了它的優(yōu)點(diǎn)和作用就說(shuō)到這吧,肯定還有很多,我也就不知道了。

關(guān)于單例的問(wèn)題大家可以看看這篇文章

http://www.zilongshanren.com/cocos2d-x-design-pattern-singleton1/

回到主題,首先我們粘貼一段代碼。

 


第一步:復(fù)制黏貼

在魔塔的源代碼中有一個(gè)單例類的模板,無(wú)腦的復(fù)制黏貼,這個(gè)模板是動(dòng)畫管理器AnimationManager的基類,這個(gè)有什么用呢,其實(shí)用處很簡(jiǎn)單就是一個(gè)單例的模板讓繼承他的子類都變成單例模式,下次再做別的游戲時(shí)用到直接繼承,多方便。當(dāng)然你也可以不用這個(gè)模板,直接讓AnimationManager變成單例類。

 

  1. #ifndef _SINGLETON_H 
  2. #define _SINGLETON_H 
  3.  
  4. template <class T> 
  5. class Singleton 
  6. public
  7.     //獲取類的唯一實(shí)例 
  8.     static inline T* instance(); 
  9.     //釋放類的唯一實(shí)例 
  10.     void release(); 
  11. protected
  12.     Singleton(void){} 
  13.     ~Singleton(void){} 
  14.     static T* _instance; 
  15. }; 
  16.  
  17. template <class T> 
  18. inline T* Singleton<T>::instance() 
  19.     if(!_instance) 
  20.         _instance = new T; 
  21.     return _instance; 
  22.  
  23. template <class T> 
  24. void Singleton<T>::release() 
  25.     if (!_instance) 
  26.         return
  27.     delete _instance; 
  28.     _instance = 0; 
  29.  
  30. //cpp文件中需要先聲明靜態(tài)變量 
  31. #define DECLARE_SINGLETON_MEMBER(_Ty)   \ 
  32.     template <> _Ty* Singleton<_Ty>::_instance = NULL; 
  33.  
  34. #endif//_SINGLETON_H 
好了這樣我們的標(biāo)準(zhǔn)的單例基類就寫好了,記住這是一個(gè).h文件

 注意:

1.子類要在子類的cpp文件開始寫上DECLARE_SINGLETON_MEMBER(子類的名字)

2.子類最好寫上構(gòu)造和析構(gòu)函數(shù)

 


第二步:編寫動(dòng)畫管理器AnimationManager類

先看代碼:

AnimationManager.h

  1. #ifndef _ANIMATION_MANAGER_H_ 
  2. #define _ANIMATION_MANAGER_H_ 
  3.  
  4. #include "cocos2d.h" 
  5. #include "Singleton.h" 
  6. #include "type.h" 
  7. using namespace cocos2d; 
  8.  
  9. class AnimationManager : public Singleton<AnimationManager> 
  10. public
  11.     AnimationManager(); 
  12.     ~AnimationManager(); 
  13.     //初始化動(dòng)畫模版緩存表 
  14.     bool initAnimationMap(); 
  15.     //根據(jù)名字得到一個(gè)動(dòng)畫模板 
  16.     CCAnimation* getAnimation(int key); 
  17.     //創(chuàng)建一個(gè)動(dòng)畫實(shí)例 
  18.     CCAnimate* createAnimate(int key); 
  19.     //創(chuàng)建一個(gè)動(dòng)畫實(shí)例 
  20.     CCAnimate* createAnimate(const char* key); 
  21.     //清空所有緩存 
  22.     void releaseAllCacha(); 
  23. protected
  24.     //加載勇士行走動(dòng)畫模版 
  25.     CCAnimation* createAnimationByDirection(HeroDirection direction); 
  26. }; 
  27. //定義動(dòng)畫管理器實(shí)例的別名 
  28. #define sAnimationMgr AnimationManager::instance() 
  29.  
  30. #endif 

AnimationManager.cpp

 

  1. #include "AnimationManager.h" 
  2. DECLARE_SINGLETON_MEMBER(AnimationManager); 
  3.  
  4.  
  5.  
  6. AnimationManager::AnimationManager() 
  7.  
  8.  
  9. AnimationManager::~AnimationManager() 
  10.  
  11.  
  12. bool AnimationManager::initAnimationMap() 
  13.     bool sRet = false
  14.     do  
  15.     { 
  16.         //1.添加圖片進(jìn)內(nèi)存 
  17.         CCTexture2D * creepTexture = CCTextureCache::sharedTextureCache()->addImage("boy.png"); 
  18.         CC_BREAK_IF(!creepTexture); 
  19.         //2.加載各個(gè)方向的敵人動(dòng)畫 
  20.         char temp[20]; 
  21.         sprintf(temp, "%d", aDown); 
  22.         //加載勇士向下走的動(dòng)畫 
  23.         CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kDown), temp); 
  24.         sprintf(temp, "%d", aRight); 
  25.         //加載勇士向右走的動(dòng)畫 
  26.         CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kRight), temp); 
  27.         sprintf(temp, "%d", aLeft); 
  28.         //加載勇士向左走的動(dòng)畫 
  29.         CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kLeft), temp); 
  30.         sprintf(temp, "%d", aUp); 
  31.         //加載勇士向上走的動(dòng)畫 
  32.         CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kUp), temp); 
  33.  
  34.         sRet = true
  35.     } while (0); 
  36.     return sRet; 
  37.  
  38. CCAnimation* AnimationManager::getAnimation( int key ) 
  39.     char temp[20]; 
  40.     sprintf(temp, "%d", key); 
  41.     return CCAnimationCache::sharedAnimationCache()->animationByName(temp); 
  42.  
  43. CCAnimate* AnimationManager::createAnimate( int key ) 
  44.     //獲取指定動(dòng)畫模版 
  45.     CCAnimation* anim = getAnimation(key); 
  46.     if(anim) 
  47.     { 
  48.         //根據(jù)動(dòng)畫模版生成一個(gè)動(dòng)畫實(shí)例 
  49.     //  return cocos2d::CCAnimate::actionWithAnimation(anim); 
  50.         return cocos2d::CCAnimate::create(anim); 
  51.     } 
  52.     return NULL; 
  53.  
  54. CCAnimate* AnimationManager::createAnimate( const char* key ) 
  55.     //獲取指定動(dòng)畫模版 
  56.     CCAnimation* anim = CCAnimationCache::sharedAnimationCache()->animationByName(key); 
  57.     if(anim) 
  58.     { 
  59.         //根據(jù)動(dòng)畫模版生成一個(gè)動(dòng)畫實(shí)例 
  60.         return cocos2d::CCAnimate::create(anim); 
  61.     } 
  62.     return NULL; 
  63.  
  64. CCAnimation* AnimationManager::createAnimationByDirection( HeroDirection direction ) 
  65.     //3.生產(chǎn)剪切動(dòng)畫 
  66.     CCTexture2D * creepTexture=CCTextureCache::sharedTextureCache()->textureForKey("boy.png"); 
  67.     CCSpriteFrame *frame0, *frame1, *frame2, *frame3; 
  68.     frame0 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*0, 95*direction, 47, 95)); 
  69.     frame1 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*1, 95*direction, 47, 95)); 
  70.     frame2 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*2, 95*direction, 47, 95)); 
  71.     frame3 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*3, 95*direction, 47, 95)); 
  72.     CCArray * animFrames = new CCArray(4); 
  73.     animFrames->addObject(frame0); 
  74.     animFrames->addObject(frame1); 
  75.     animFrames->addObject(frame2); 
  76.     animFrames->addObject(frame3); 
  77.     CC_SAFE_RETAIN(animFrames); 
  78.     CCAnimation * animation= CCAnimation::createWithSpriteFrames(animFrames,0.5f); 
  79.  
  80.     animFrames->release(); 
  81.     return animation; 
  82.  
  83. void AnimationManager::releaseAllCacha() 
  84.      

 

建立完這個(gè)動(dòng)畫管理單例后,用法也是很簡(jiǎn)單,只需要調(diào)用那幾個(gè)getXXXX 就行,當(dāng)然這個(gè)動(dòng)畫管理器功能還比較簡(jiǎn)單,因?yàn)槲沂莻€(gè)新手所以不能一下吧所有的功能都寫上,還是先用到哪些寫那些。

 


第三步:全局?jǐn)?shù)據(jù)

之后我們?cè)诮⒁粋€(gè)單例類,來(lái)保存一些全局?jǐn)?shù)據(jù)

HWorld.h

  1. #ifndef __HWORLD_H__ 
  2. #define __HWORLD_H__ 
  3.  
  4. #include "cocos2d.h" 
  5. #include "type.h" 
  6. #include "Singleton.h" 
  7. using namespace cocos2d; 
  8. class HWorld: 
  9.     public Singleton<HWorld> 
  10. public
  11.     HWorld(); 
  12.     ~HWorld(); 
  13.     //初始化 
  14.     bool initHWorld(); 
  15.     //清空所有緩存 
  16.     void releaseAll(); 
  17. public
  18.     //路點(diǎn)數(shù)組 
  19.     CCPointArray * _wayPoint;    
  20. }; 
  21. //定義動(dòng)畫管理器實(shí)例的別名 
  22. #define sHWorld HWorld::instance() 
  23. #endif

HWorld.cpp

 

  1. #include "HWorld.h"  
  2. DECLARE_SINGLETON_MEMBER(HWorld);  
  3.  
  4. bool HWorld::initHWorld()  
  5. {  
  6.     return true;  
  7. }  
  8.  
  9. void HWorld::releaseAll()  
  10. {  
  11. }  
  12.  
  13. HWorld::HWorld()  
  14. {  
  15. }  
  16.  
  17. HWorld::~HWorld()  
  18. {  
  19. }  

目前這個(gè)類里面之后一個(gè)路點(diǎn)的數(shù)組,這個(gè)數(shù)組中保存了地圖上需要轉(zhuǎn)彎地方的坐標(biāo),其他的東西以后再添加。

當(dāng)然還有我們的枚舉,把它放在一個(gè).h文件中,方便查看

type.h

 

  1. #ifndef __TYPE_H__  
  2. #define __TYPE_H__  
  3.  
  4. typedef enum {  
  5.     kDown =  0,//向下方向  
  6.     kLeft = 1,//向左方向  
  7.     kRight= 2,//向右方向  
  8.     kUp = 3,//向上方向  
  9.     kNormal,  
  10. } HeroDirection;//勇士方向  
  11.  
  12.  
  13. typedef enum 
  14. {  
  15.     aDown = 0,//向下行走動(dòng)畫  
  16.     aLeft,//向左行走動(dòng)畫  
  17.     aRight,//向右行走動(dòng)畫  
  18.     aUp,//向上行走動(dòng)畫  
  19.     aFight,//刀光動(dòng)畫  
  20. } AnimationKey;//動(dòng)畫模版鍵值  
  21. //定義怪物的類型  
  22. typedef enum 
  23. {  
  24.     kHighBlood=0,//高血量怪物  
  25.     kQuick = 1,  
  26.  
  27. }CreepType;  
  28. //----------------定義一個(gè)creep中所用到的tag--------  
  29. typedef enum 
  30. {  
  31.     kSprite =0,//精靈的標(biāo)簽  
  32.     kSpeedAction = 1,//控制速度的action所用的標(biāo)簽  
  33.     kFoveverAction = 2,//控制精靈幀的action標(biāo)簽  
  34. }CreepTag;  
  35.  enum   
  36. {  
  37.     kZMap = 0,//地圖的zOrder  
  38.     kZNPC,  
  39.     kZTeleport,  
  40.     kZHero,//勇士精靈的zOrder  
  41.     kZTip,//提示信息的zOrder  
  42. };//GameLayer中各部分的顯示zOrder及tag  
  43. #endif 

 


好了,生成一下。

下一篇會(huì)讓我們的敵人動(dòng)起來(lái)

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI