您好,登錄后才能下訂單哦!
好了它的優(yōu)點(diǎn)和作用就說(shuō)到這吧,肯定還有很多,我也就不知道了。
關(guān)于單例的問(wèn)題大家可以看看這篇文章 http://www.zilongshanren.com/cocos2d-x-design-pattern-singleton1/ |
回到主題,首先我們粘貼一段代碼。
在魔塔的源代碼中有一個(gè)單例類的模板,無(wú)腦的復(fù)制黏貼,這個(gè)模板是動(dòng)畫管理器AnimationManager的基類,這個(gè)有什么用呢,其實(shí)用處很簡(jiǎn)單就是一個(gè)單例的模板讓繼承他的子類都變成單例模式,下次再做別的游戲時(shí)用到直接繼承,多方便。當(dāng)然你也可以不用這個(gè)模板,直接讓AnimationManager變成單例類。
- #ifndef _SINGLETON_H
- #define _SINGLETON_H
- template <class T>
- class Singleton
- {
- public:
- //獲取類的唯一實(shí)例
- static inline T* instance();
- //釋放類的唯一實(shí)例
- void release();
- protected:
- Singleton(void){}
- ~Singleton(void){}
- static T* _instance;
- };
- template <class T>
- inline T* Singleton<T>::instance()
- {
- if(!_instance)
- _instance = new T;
- return _instance;
- }
- template <class T>
- void Singleton<T>::release()
- {
- if (!_instance)
- return;
- delete _instance;
- _instance = 0;
- }
- //cpp文件中需要先聲明靜態(tài)變量
- #define DECLARE_SINGLETON_MEMBER(_Ty) \
- template <> _Ty* Singleton<_Ty>::_instance = NULL;
- #endif//_SINGLETON_H
注意: 1.子類要在子類的cpp文件開始寫上DECLARE_SINGLETON_MEMBER(子類的名字) 2.子類最好寫上構(gòu)造和析構(gòu)函數(shù) |
- #ifndef _ANIMATION_MANAGER_H_
- #define _ANIMATION_MANAGER_H_
- #include "cocos2d.h"
- #include "Singleton.h"
- #include "type.h"
- using namespace cocos2d;
- class AnimationManager : public Singleton<AnimationManager>
- {
- public:
- AnimationManager();
- ~AnimationManager();
- //初始化動(dòng)畫模版緩存表
- bool initAnimationMap();
- //根據(jù)名字得到一個(gè)動(dòng)畫模板
- CCAnimation* getAnimation(int key);
- //創(chuàng)建一個(gè)動(dòng)畫實(shí)例
- CCAnimate* createAnimate(int key);
- //創(chuàng)建一個(gè)動(dòng)畫實(shí)例
- CCAnimate* createAnimate(const char* key);
- //清空所有緩存
- void releaseAllCacha();
- protected:
- //加載勇士行走動(dòng)畫模版
- CCAnimation* createAnimationByDirection(HeroDirection direction);
- };
- //定義動(dòng)畫管理器實(shí)例的別名
- #define sAnimationMgr AnimationManager::instance()
- #endif
- #include "AnimationManager.h"
- DECLARE_SINGLETON_MEMBER(AnimationManager);
- AnimationManager::AnimationManager()
- {
- }
- AnimationManager::~AnimationManager()
- {
- }
- bool AnimationManager::initAnimationMap()
- {
- bool sRet = false;
- do
- {
- //1.添加圖片進(jìn)內(nèi)存
- CCTexture2D * creepTexture = CCTextureCache::sharedTextureCache()->addImage("boy.png");
- CC_BREAK_IF(!creepTexture);
- //2.加載各個(gè)方向的敵人動(dòng)畫
- char temp[20];
- sprintf(temp, "%d", aDown);
- //加載勇士向下走的動(dòng)畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kDown), temp);
- sprintf(temp, "%d", aRight);
- //加載勇士向右走的動(dòng)畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kRight), temp);
- sprintf(temp, "%d", aLeft);
- //加載勇士向左走的動(dòng)畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kLeft), temp);
- sprintf(temp, "%d", aUp);
- //加載勇士向上走的動(dòng)畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createAnimationByDirection(kUp), temp);
- sRet = true;
- } while (0);
- return sRet;
- }
- CCAnimation* AnimationManager::getAnimation( int key )
- {
- char temp[20];
- sprintf(temp, "%d", key);
- return CCAnimationCache::sharedAnimationCache()->animationByName(temp);
- }
- CCAnimate* AnimationManager::createAnimate( int key )
- {
- //獲取指定動(dòng)畫模版
- CCAnimation* anim = getAnimation(key);
- if(anim)
- {
- //根據(jù)動(dòng)畫模版生成一個(gè)動(dòng)畫實(shí)例
- // return cocos2d::CCAnimate::actionWithAnimation(anim);
- return cocos2d::CCAnimate::create(anim);
- }
- return NULL;
- }
- CCAnimate* AnimationManager::createAnimate( const char* key )
- {
- //獲取指定動(dòng)畫模版
- CCAnimation* anim = CCAnimationCache::sharedAnimationCache()->animationByName(key);
- if(anim)
- {
- //根據(jù)動(dòng)畫模版生成一個(gè)動(dòng)畫實(shí)例
- return cocos2d::CCAnimate::create(anim);
- }
- return NULL;
- }
- CCAnimation* AnimationManager::createAnimationByDirection( HeroDirection direction )
- {
- //3.生產(chǎn)剪切動(dòng)畫
- CCTexture2D * creepTexture=CCTextureCache::sharedTextureCache()->textureForKey("boy.png");
- CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
- frame0 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*0, 95*direction, 47, 95));
- frame1 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*1, 95*direction, 47, 95));
- frame2 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*2, 95*direction, 47, 95));
- frame3 = CCSpriteFrame::createWithTexture(creepTexture, cocos2d::CCRectMake(47*3, 95*direction, 47, 95));
- CCArray * animFrames = new CCArray(4);
- animFrames->addObject(frame0);
- animFrames->addObject(frame1);
- animFrames->addObject(frame2);
- animFrames->addObject(frame3);
- CC_SAFE_RETAIN(animFrames);
- CCAnimation * animation= CCAnimation::createWithSpriteFrames(animFrames,0.5f);
- animFrames->release();
- return animation;
- }
- void AnimationManager::releaseAllCacha()
- {
- }
建立完這個(gè)動(dòng)畫管理單例后,用法也是很簡(jiǎn)單,只需要調(diào)用那幾個(gè)getXXXX 就行,當(dāng)然這個(gè)動(dòng)畫管理器功能還比較簡(jiǎn)單,因?yàn)槲沂莻€(gè)新手所以不能一下吧所有的功能都寫上,還是先用到哪些寫那些。
之后我們?cè)诮⒁粋€(gè)單例類,來(lái)保存一些全局?jǐn)?shù)據(jù)
HWorld.h
- #ifndef __HWORLD_H__
- #define __HWORLD_H__
- #include "cocos2d.h"
- #include "type.h"
- #include "Singleton.h"
- using namespace cocos2d;
- class HWorld:
- public Singleton<HWorld>
- {
- public:
- HWorld();
- ~HWorld();
- //初始化
- bool initHWorld();
- //清空所有緩存
- void releaseAll();
- public:
- //路點(diǎn)數(shù)組
- CCPointArray * _wayPoint;
- };
- //定義動(dòng)畫管理器實(shí)例的別名
- #define sHWorld HWorld::instance()
- #endif
HWorld.cpp
- #include "HWorld.h"
- DECLARE_SINGLETON_MEMBER(HWorld);
- bool HWorld::initHWorld()
- {
- return true;
- }
- void HWorld::releaseAll()
- {
- }
- HWorld::HWorld()
- {
- }
- HWorld::~HWorld()
- {
- }
目前這個(gè)類里面之后一個(gè)路點(diǎn)的數(shù)組,這個(gè)數(shù)組中保存了地圖上需要轉(zhuǎn)彎地方的坐標(biāo),其他的東西以后再添加。
當(dāng)然還有我們的枚舉,把它放在一個(gè).h文件中,方便查看
type.h
- #ifndef __TYPE_H__
- #define __TYPE_H__
- typedef enum {
- kDown = 0,//向下方向
- kLeft = 1,//向左方向
- kRight= 2,//向右方向
- kUp = 3,//向上方向
- kNormal,
- } HeroDirection;//勇士方向
- typedef enum
- {
- aDown = 0,//向下行走動(dòng)畫
- aLeft,//向左行走動(dòng)畫
- aRight,//向右行走動(dòng)畫
- aUp,//向上行走動(dòng)畫
- aFight,//刀光動(dòng)畫
- } AnimationKey;//動(dòng)畫模版鍵值
- //定義怪物的類型
- typedef enum
- {
- kHighBlood=0,//高血量怪物
- kQuick = 1,
- }CreepType;
- //----------------定義一個(gè)creep中所用到的tag--------
- typedef enum
- {
- kSprite =0,//精靈的標(biāo)簽
- kSpeedAction = 1,//控制速度的action所用的標(biāo)簽
- kFoveverAction = 2,//控制精靈幀的action標(biāo)簽
- }CreepTag;
- enum
- {
- kZMap = 0,//地圖的zOrder
- kZNPC,
- kZTeleport,
- kZHero,//勇士精靈的zOrder
- kZTip,//提示信息的zOrder
- };//GameLayer中各部分的顯示zOrder及tag
- #endif
好了,生成一下。
下一篇會(huì)讓我們的敵人動(dòng)起來(lái)
免責(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)容。