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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。