O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding actions to sprites

We already saw Actions in action while animating and repeating the player animation. However, there are a lot more additional actions that you can perform. Further, you can play these actions together in a sequence.

Getting started

Let's first take a look at a simple action in which we will move the hero along the x axis by half the width of the screen, and move it down by one-quarter the height of the screen from the center in the y direction.

How to do it…

After we have added the hero to MainScene, we will add the following code:

CGPointfinalPos = CGPointMake(center.x + winSize.width/4, center.y - winSize.height/4); CCActionFiniteTime* actionMove = [CCActionMoveToactionWithDuration:1.0position:finalPos]; [herorunAction:actionMove]; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required