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.

 
135
Kudos
 
135
Kudos

Now read this

Cocos2D-x Tutorials Part 5: Schedule update and Actions Pause and Resume

In the previous tutorial we saw how to create actions and run them in single order or sequentially. In this tutorial we will see how to schedule a function that will be called on every frame and how can we pause and resume the whole... Continue →