Is it possible to pause and resume actions?

Is it possible to pause and resume actions?


This is only theory. Eventually I will do a test to see if this is viable. Meanwhile, experience of other users of the engine is very welcome.

The theory: as actions doesn’t have a pause/resume interface, one way of achieve this if you ever need such functionality, may be:

  1. track the time elapsed since an action was ran, or get the time elapsed using action’s interface (ActionInterval::getElapsed() looks promising). Then, when resuming, create a new action with exact the same configuration and call new_action->step(prev_action_elapsed).

  2. Or maintain a reference to a running action, through a RefPtr, so when you call some_node->stopAction you can re run the action from where it was interrupted before (I’m about to test if actions remember its elapsed time after stopped, or if they rewind).


We should write about this too in our docs.


@bardo I’m not sure what you exactly need but Check out the ActionManager class.

Also node has a functions called pause and resume which apply to actions.

node->pause(); node->resume();


@bilalmirza, it was easier than I thought then. Thank you.

@slackmoehrle, that would be welcome. For the most part documentation is good, api ref is complete. The functions names are actually self explanatory (pause/resume), I just missed them this time.

I’m in a situation where if a condition is met, a moving sprite must stop, and resume its movement at a later time. I’m also using a not linear interpolation, it’s important action state doesn’t reset, I don’t want the sprite to start slow again. That must be desirable for most situations but not for this case. It must look like time stopped then resumed. Maybe I can post an animated gif but I think is not required as the question is answered.

off-topic: I visited some questions on stackoverflow about cocos2d and came with the idea that actions weren’t pausable. May that be the case for some old releases?


I don’t think so its been there as far as I can remember


For example:

The asking user asked for a method to pause/resume, and he got some alternative implementation for what he needed.

Well, It doesn’t matter.


@Bardo I have a similar situation where I need to save the internal action state.

But the logic of pause() and resume() seems not to work that way.

I pause() a node and all of it’s children recursively and that works.

But when I resume() a node and all of it’s children recursively the actions don’t resume.

For example a MoveTo action on a sprite, so I pause(), and you see a sprite in the middle of the display.

When I resume it just stays there and doesn’t continue moving.

Do you know a way around this?