Reversing an animation before animation is done

Reversing an animation before animation is done
0.0 0


I have a scenario of a door which is opened when something happens. However that “something” may be gone before the door was completely opened and the door should now shut down from the exact frame it has been stopped.

so assuming I have 5 sprites for the door to open. assume the following scenario

frame 1 -> frame 2 -> frame 3 -> something break the door from been open reversing -> frame 2 -> frame 1 -> door closed.

How can I achieve this in cocos2dx ?

Feels like a quite standard behiavor for many other scenarios.


(hopefully someone knows how to do this with either the current Animate action, or has a link to pre-existing code of an Action-derived custom animate class, or otherwise has an answer whereby you can just plug something in without thought and get it working)

Otherwise, here’s my quick thought(s):

It may be possible to do this with the various Action derived classes, but I’d recommend just doing your own animation. It’s not too terribly difficult. Just copy some of the code from CCAnimate.cpp. The other idea would be to derive a custom MyCustomAnimate.cpp and then more easily code in the reversing and associated calculations.

In this specific case you may be able to easily derive from Animate and add your own ‘reverse animation’ functionality without too much work. The key is to have a flag where you either add/sub the delta time based on whether reversed or not ( t = reversed ? t - dt : t + dt ) , or you determine the current frame index based on whether reserved ( curIndex = reversed ? nFrames - calculatedFrameIndex : calculatedFrameIndex ).

You can also write your own animation update code inside the game loop scheduled update or a given Node-derived entity’s class’s scheduled update method. Just use similar code as in Animate where it takes an elapsedTime value and determines the currentIndex from that. Then have a ‘run in reverse’ flag such that instead of adding deltaTime to elapsedTime subtract it instead.

There are probably a hundred ways to do this. I often find, however, that people try to use the Action system for everything and while it’s a great set of functionality it’s limited and restricted and whenever you hit the limits you should almost always move into your custom coded and updated system usually by not-deriving from ActionInterval (or whichever).

Anyway, hope these non-concrete examples give you an idea of how to move forward.


Yes. some good ideas here. Thanks for the reply.

And yes, I was hoping to keep working on the action method but sometimes it just makes things much harder.

My first thought was to create a new Animation object which contains the relevant animation frames and just stop the original action and start a new one with the new Animation object. in theory it should work but performance wise I am not sure .