CocosBuilderのアニメーションが終わったタイミングでプログラム実行

CocosBuilderで作成したアニメーションが終わったタイミングで特定の処理を実行する必要があり、実装方法を調べたので記しておきます。
cocos2d-xの例です。

参考にしたサイト
Plunge Interactive | Game outsourcing, app development outsourcing and games for iOS, Android and Windows

今回使用したバージョン

  • cocos2d-2.0-x-2.0.4
  • CocosBuilder2.1


アニメーションはCocosBuilder側で作成するので、CocosBuilder側でアニメーション終了時にキックするメソッドを指定できるのかと思っていたのですが、
実はプログラム側でDelegateが呼ばれるようになっていました。


実装方法

HogeLayer.h

// CCBAnimationManagerDelegateを継承
class HogeLayer: public CCLayer, public CCBAnimationManagerDelegate
{
public:
	CCBAnimationManager* mAnimationManager;
	
	HogeLayer();
	virtual ~HogeLayer();
	
	virtual bool init();
	
	// アニメーション終了時に呼ばれるDelegate
	virtual void completedAnimationSequenceNamed(const char *name);
	
	CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(TitleLayer,create);
	
	void setAnimationManager(CCBAnimationManager *pAnimationManager);
};

HogeLayer.cpp

// アニメーション終了時の処理
void HogeLayer::completedAnimationSequenceNamed(const char *name)
{
	CCLOG("%s",name);

	// アニメーションの名前によって処理を分ける
	if(strcmp(name,"StartAnimation") == 0)
	{
		// 処理
	}
}

void HogeLayer::setAnimationManager(cocos2d::extension::CCBAnimationManager *pAnimationManager)
{
	CC_SAFE_RELEASE_NULL(mAnimationManager);
    mAnimationManager = pAnimationManager;
    CC_SAFE_RETAIN(mAnimationManager);
	
	// Delegate設定
	mAnimationManager->setDelegate(this);
}

アニメーションが終了したタイミングでcompletedAnimationSequenceNamedが呼ばれるようになります。
どのアニメーションが終了しても呼ばれるので、特定のアニメーション終了時のみ実行したい場合は、名前で判別する必要があります。

なお、アニメーションA→アニメーションA→アニメーションA・・・のようにループしたり、
アニメーションA→アニメーションB→アニメーションCのようにチェーンしても、それぞれの終了時に呼び出されます。