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:

Touch_and_Move.h:

Touch_and_Move.cpp:

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.

 
134
Kudos
 
134
Kudos

Now read this

Cocos2D-x Tutorials Part 1: Adding Your Own Layer

In this tutorial I am assuming that you have already installed cocos2d-x in your system if not then follow this link, its not that hard: create a new project in cocos2d-x Also we will use C++11 and Cocos2D-x V3 for the tutorials. Now... Continue →