cocos2d-x / XCode によるスマートフォンのアプリ開発(c++)
// DQ風ウィンドウマネージャ "SSMsgBoxDQ.h" class SSMsgBoxDQ : public CCObject { public: SSMsgBoxDQ(void); virtual ~SSMsgBoxDQ(void); static SSMsgBoxDQ* createWithArgs(kSSMsgBoxType type, float fontSize, int cntCols, int cntRows, int cntMaxTxtLen); CCLabelTTF* getLabel(){ return m_label; }; : protected: CCLabelTTF* m_label; // ラベル :
// DQ風ウィンドウマネージャ "SSMsgBoxDQ.cpp" SSMsgBoxDQ::SSMsgBoxDQ(void) { // コンストラクタ CCLog(__FUNCTION__); } SSMsgBoxDQ::~SSMsgBoxDQ(void) { // デストラクタ CC_SAFE_RELEASE(m_label); } SSMsgBoxDQ* SSMsgBoxDQ::createWithArgs(kSSMsgBoxType type, float fontSize, int cntCols, int cntRows, int cntMaxTxtLen) { // 初期化関数 SSMsgBoxDQ *pRet = new SSMsgBoxDQ(); if (pRet && pRet->initWithArgs(type, fontSize, cntCols, cntRows, cntMaxTxtLen)) { pRet->autorelease(); pRet->retain(); //※ 後に追加した部分 ※ return pRet; } else { CC_SAFE_DELETE(pRet); return NULL; } }
void Game::Initialize() { : // 前略 m_statusBox = SSMsgBoxDQ::createWithArgs(kSSMsgBoxTypeStatus, 13, m_cntPlayers, 3, 7); // メッセージボックス生成 : } void Game::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { : // 前略 // モンスターの HPやMPを更新 m_statusBox->updateVal(i, 1, tmpMonster->hp()); m_statusBox->updateVal(i, 2, tmpMonster->mp()); : }
The logic of CCAutoreleasePool is that, when you call object->autorelease(), this object is put into the autorelease pool. The pool can help you to retain this object's lifecycle till the end of current message loop. At the end of current message loop, if this object hasn't been retained by any other class/container, it will be released automaticaly. For example, layer->addChild(sprite), the sprite is added to the 'children list' of the layer, its lifecycle is retained till the release of layer, but not the end of current message loop.
No comments:
Post a Comment