Using CCActionSpeed in CCActionRepeat

Home » Development » Cocos2D » Using CCActionSpeed in CCActionRepeat

In this developer blog post, I am going to demonstrate a design pattern that will allow you to dynamically adjust the speed of a repeating animation sequence in Cocos2D v3.x .

Basic Animation Sequence

A common requirement of an animation sequence is to dynamically adjust the speed of the sequence. Let’s use an heart beat as an example for a health meter and the more the health reduces, the faster the heart beat animates. So a typical heart beat animation sequence might look like this:

So I decrease the size of the sprite by %50 using CCActionScaleTo, then resize it back to %100 using a nonlinear timing curve (CCActionEaseIn) and finally queue them up into a CCActionSequence.

Repeated Animation Sequence

To apply this sequence to a sprite, you would want to run it indefinitely and so you would run it like this using CCActionRepeat or CCActionRepeatFoever:

This will animate the heart sprite at a regular rate, pulsating as if to show it is beating.

Cocos2D offers an action that will allow the developer to adjust the speed of another action called CCActionSpeed. CCActionSpeed normalises the action it is applied to. It is controlled by the “speed” variable and so a value of 1.0 means that the sequence will take 2 seconds to complete one iteration, changing the speed value to 2.0 means that the sequence will take 1.0 seconds to complete while a speed value of 0.5 will take 4.0 seconds to complete.

Variable Speed Animation Sequence

To apply CCActionSpeed to a sequence, you might use the following code:

This will run the animation only once but obviously we need to let this run continually and adjust the speed variable for the CCActionSpeed object. I new comer to Cocos2D would naturally assume that you can do the following:

This will not work and Xcode will not return a compiler error. The class CCActionSpeed inherits from CCAction but CCActionRepeat(Forever) require an action that inherits from CCActionInterval. So the repeat action will not honour the code inside the speed object. So then new comers try the opposite:

This won’t work for the exact same reason.

Repeated Variable Speed Animation Sequence

My approach to implement the speed adjustable repeated sequence was to use this design pattern:

So basically I am not using CCActionRepeat(Forever) but instead re-applying the animation using CCActionCallFunc with the speed adjusted to the new rate every time the sequence completes. This is not an ideal solution to the issue but it certainly works.[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
By | 2017-02-14T04:15:21+00:00 January 1st, 2015|Cocos2D, Development|0 Comments

About the Author:

Software Developer and Founder of Rotate Gears. Nader is an Electrical Engineer in Computer Systems. He has a solid background in game development using Cocos2D having contributed code to the project and helped author a book on the topic.

Leave A Comment