Loop an animation programatically in Cocos2d, like chaining the animation to itself in Spritebuilder

178 views Asked by At

Using Spritebuilder, I have chained timelines to themselves to create a loop (eg- for walking), but in some cases I would like to use Timelines that are not chained, and loop them programatically. I believe I may be able to use CCActionSequence to do this but have not been able to find an example. Can anyone provide an example please?

1

There are 1 answers

1
Pontus Armini On BEST ANSWER

Here's a late answer (you've probably found some kind of solution, but I'm posting anyway, for future reference): To my knowledge there isn't really any exposed way of achieving what you want to do in the current version of Cocos2d (v.3.4). And at the moment it doesn't look like this is going to change in v.4.0 (I'm making this assumption based on the current state of the develop branch on Github, but this can of course change).

That said, if you are willing to accept a little bit of hacking, I've created a category on CCAnimationManager that is exposing the CCBSequences (i.e. the timelines) created in Spritebuilder, thus making it possible to change which timelines should be chained together in code. When using this category you could do something like this:

//Get the timeline that you want to change 
CCBSequence * timeline = [_myNode.animationManager sequenceForSequenceNamed:@"TimeLineToLoop"];

//Set the chainedSequenceId to the timelines own id to make it loop infinitely.
timeline.chainedSequenceId = timeline.sequenceId;

//Run the loop
[_myNode.animationManager runAnimationsForSequenceNamed:timeline.name];