Wait for a specific animation to finish

Wait for a specific animation to finish
0.0 0


I’m curious if there is a better way to wait for some animation to finish than using callbacks. Coroutines would do this but I can’t find a standard implementation in C++. The inability to tap into cocos2dx main loop makes writing my own blocking event dispatch difficult as well. How do people do this in cocos2dx? Callbacks will do except that I have a ton of code to run, which waits on some other animation and then calls something else. I’d rather not have a callback hell. Thanks.


We can only use callback so far.
Another choice is to use integrate Boost.Coroutine by yourself.


Too bad there are quite a few issues with Boost.Coroutine. It’s not yet officially released and has bad cross compiler support. One of its own test cases, test_unwind, fails when built with clang(works fine in gcc-4.6 though). Looks like I’m stuck :(.


You can implement your own custom actions as alternative to callbacks. They can be sequenced. Try writing actions as generic as you can and pass specific parameters on creation. Works great for me.


Too bad this only works for animation sequence, not abitrary code. A lot of my delayed actions are actually calculations and if I go the callback action style I’ll have a gazillion callbacks, or just tons of lambdas scattered all over place… Just wrote my own coroutine system today and will share it if it works smoothly in production. :smiley:



Take a look at this code snippet:

        cocos2d::CCAnimate* animate = cocos2d::CCAnimate::create(animation.get());
        cocos2d::CCCallFunc* freeSpriteAction = cocos2d::CCCallFuncN::create(this, static_cast(&SpawnedAnimation::OnEndAnimation));
        cocos2d::CCFiniteTimeAction* animationSequence = cocos2d::CCSequence::create(animate, freeSpriteAction, nullptr);

The SpawnedAnimation::OnEndAnimation is a simple plain method, which can be used for whatever you like.


I’m aware that I could do this. Again what I meant is the difficulty of doing this recursively inside callback’s callbacks. Nvm :slight_smile:


Hi, can you share your solution please?
Having same problem as yours.