您好,登錄后才能下訂單哦!
Cocos2d-x中的CCObject類及其派生類,使用autorelease()方法,將自身交托于CCPoolManager管理器進行管理,都可以使用retain()方法來使自身的引用計數加一,使用release()方法來使自身的引用計數減一,當引用計數為0的時候,CCPoolManager管理器就會將其刪除釋放。
類
所有實例化Cocos2d-x里面的以CCObject為基類的類時,都要使用其create()方法來創建對象,對于自己添加的派生類,需要通過CREATE_FUNC宏來實現create()方法,下面以《如何制作一個橫版格斗過關游戲 Cocos2d-x 2.0.4》來舉例介紹:
Hero.h
2 3 4 5 6 7 8 9 | class Hero : public ActionSprite { public: Hero(void); ~Hero(void); CREATE_FUNC(Hero); //…… }; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new __TYPE__(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = NULL; \ returnNULL; \ } \ } |
2 3 4 5 6 7 8 9 10 | class SimpleDPad : public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate { public: SimpleDPad(void); ~SimpleDPad(void); static SimpleDPad* dPadWithFile(cocos2d::CCString *fileName, float radius); bool initWithFile(cocos2d::CCString *filename, float radius); //…… }; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 | SimpleDPad* SimpleDPad::dPadWithFile(CCString *fileName, float radius) { SimpleDPad *pRet = new SimpleDPad(); if (pRet && pRet->initWithFile(fileName, radius)) { pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; returnNULL; } } |
2 3 4 5 6 7 8 9 10 | class GameLayer : public cocos2d::CCLayer, public SimpleDPadDelegate { public: GameLayer(void); ~GameLayer(void); CREATE_FUNC(GameLayer); //…… CC_SYNTHESIZE_RETAIN(cocos2d::CCArray*, _robots, Robots); }; |
2 3 4 5 6 7 8 9 10 11 | GameLayer::GameLayer(void) { //…… _robots = NULL; } GameLayer::~GameLayer(void) { //…… CC_SAFE_RELEASE_NULL(_robots); } |
2 3 4 5 6 7 8 9 | class ActionSprite : public cocos2d::CCSprite { public: ActionSprite(void); ~ActionSprite(void); //…… CC_SYNTHESIZE_RETAIN(cocos2d::CCAction*, _attackAction, AttackAction); }; |
2 3 4 5 6 7 8 9 10 11 12 13 | bool Hero::init() { bool bRet = false; do { //…… this->setAttackAction(CCSequence::create(CCAnimate::create(attackAnimation), CCCallFunc::create(this, callfunc_selector(Hero::idle)), NULL)); //…… } while (0); return bRet; } |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | CCCallFunc * CCCallFunc::create(CCObject* pSelectorTarget, SEL_CallFunc selector) { CCCallFunc *pRet = new CCCallFunc(); if (pRet && pRet->initWithTarget(pSelectorTarget)) { pRet->m_pCallFunc = selector; pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); returnNULL; } bool CCCallFunc::initWithTarget(CCObject* pSelectorTarget) { if (pSelectorTarget) { pSelectorTarget->retain(); } if (m_pSelectorTarget) { m_pSelectorTarget->release(); } m_pSelectorTarget = pSelectorTarget; returntrue; } |
2 3 4 | virtual ~CCCallFunc() { CC_SAFE_RELEASE(m_pSelectorTarget); } |
2 3 4 5 6 | class ActionSprite : public cocos2d::CCSprite { public: //…… virtual void cleanup(); }; |
2 3 4 5 6 7 8 | void ActionSprite::cleanup() { CC_SAFE_RELEASE_NULL(_idleAction); CC_SAFE_RELEASE_NULL(_attackAction); CC_SAFE_RELEASE_NULL(_walkAction); CC_SAFE_RELEASE_NULL(_hurtAction); CC_SAFE_RELEASE_NULL(_knockedOutAction); CCSprite::cleanup(); } |
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。