Cocos2D-x Tutorials Part 4: Actions & Sequence

In Part 3 we were introduced with cocos2d-x actions. In this tutorial we will see in detail how to create actions and run them.

Most Actions in cocos2d-x has a format: action_By & action_To like MoveBy & MoveTo. Whats the difference between them ? well it’s simple, action_To executes only once and then stops where action_By executes the action as many times as you want.

Now lets learn to create an action.

auto jump = JumpTo::create(1.f, Point(0,0), 150, 4);

Here the first parameter is the time the action will take to complete.

The 2nd parameter take a Point where the sprite will land after completing the jump.

Third parameter takes the jump height.

Fourth parameter takes the number of jump.

Lets run the action:

ball-> runAction(jump);

Lets see the entire code:



You can create other actions similarly.

But if you want to run multiple actions on a sprite then what to do ?? Here comes Sequence into play.

lets add another action after jump:

auto scale= ScaleTo::create(1.f,2.f,2.f);

Then modify line 22 like this:

ball->runAction(Sequence::create(jump,scale, nullptr));

Sequence takes multiple action reference and then executes them one by one.
Lets run the code and see what happens.
Next we will see how to update our scene per frame and how to stop and resume an action.


Now read this

Cocos2D-x Tutorials Part 3: Moving a Sprite

In Part 1 and Part 2 we learned to add our layer and sprite. In this tutorial we will move our sprite where we touched on the screen. Or more generally speaking we will see how to apply an Action on a sprite. Also we will take a look at... Continue →