Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Overview of this book

Cocos2d is the world’s leading game development framework for developing iOS games. With the introduction of Swift and Spritebuilder, it has become easier than ever to develop the games of your dreams without much effort. With Cocos2d, you can also deploy the game on Android, thereby maximizing profit and reducing development and porting costs. The book starts off with a detailed look at how to implement sprites and animations into your game to make it livelier. You will then learn to add scenes to the game such as the gameplay scene and options scene and create menus and buttons in these scenes, as well as creating transitions between them. From there on, you will get an understanding of how to program user interactions such as tapping, holding, and swiping. You’ll then add accelerometer inputs and physics to the scene, and make objects respond back to the inputs. A game is practically incomplete without audio being added, so this will be covered next. The next section will include ways to add Artificial Intelligence to enemies in the game, allowing them to patrol, chase, and shoot in a projectile manner. You will then learn to use NSUserDefault to save and load game progress, and create and access files using JSON, Plist, and XML files for custom storage and retrieval of data. Then you will learn to add dynamic lighting to your game and will use industry-wide tools such as Texture Packer, Glyph Designer, Physics Editor, Particle Designer, and Sprite Illuminator to create more visually appealing and performance-optimized games. Towards the end of the book, we dive into Apple’s latest programming language—Swift, highlighting the major differences between Objective C and Swift. The book culminates with taking your existing game developed for iOS and porting it to Android, showing you how to install the Android Xcode plugin as well.
Table of Contents (19 chapters)
Cocos2d Cross-Platform Game Development Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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];

For convenience, I created a CGPoint called finalPos and stored the final position in it. Then, I created a variable called actionMove of the CCFiniteAction type, called the CCMoveTo function, gave it a duration of 1.0 seconds, and specified the position that I wanted to move the hero to. Finally, I called the runAction function on hero and passed in the action.

How it works…

When you run the project, the hero will be to the left of the yellow render sprite and will slowly start moving towards the lower-right corner if the render sprite is over a period of 1 second. After 1 second, when the destination location is reached, the action will stop, and the hero will be stationary again.

There's more…

Let's next create a more elaborate action and add a whole bunch of actions in a sequence and play them one after the other. Therefore, we will remove the previous code and add the following code instead:

//Actions

CGPointinitPos = hero.position;
CGPointfinalPos = CGPointMake(center.x + winSize.width/4, center.y - winSize.height/4);

CCActionFiniteTime* actionMove = [CCActionMoveToactionWithDuration:1.0position:finalPos];

CCAction *rotateBy = [CCActionRotateByactionWithDuration:2.0 angle: 180];

CCAction *tintTo=  [CCActionTintToactionWithDuration:1.0 color:[CCColorcolorWithRed:0.0fgreen:1.0blue:0.0]];

CCAction *delay = [CCActionDelayactionWithDuration:1.0];

CCAction *moveToInit = [CCActionMoveToactionWithDuration:1.0position:initPos];

CCAction *rotateBack = [CCActionRotateByactionWithDuration:2.0 angle: 180];

CCAction *tintBlue=  [CCActionTintToactionWithDuration:1.0 color:[CCColorcolorWithRed:0.0fgreen:0.0blue:1.0]];

CCAction *sequence = [CCActionSequenceactions:actionMove, rotateBy,tintTo, moveToInit, delay, rotateBack, tintBlue, nil];

[herorunAction:sequence];

Here, as I stored the final position in a variable, I will also store the initial position in a CGPoint variable called initPos, which we will use later.

The first action is the same moveTo action with which we will move the player.

Next, we will use the rotateBy action; here, we will give the duration and angle in degrees by which the object should be rotated.

After this, we will use the tintTo action, which will change the color of the object. Once again, we will give it the duration and color to which we want to the object to change. Here, we will change the color to green.

We will then call the delay action, which is used to perform an action after a delay. We will create a delay of 1 second.

Then, we will move the object back to its initial position, change the object color to blue, and rotate the object again by another 180 degrees.

We will create the CCSequence action and pass in all the actions in the order we want them to be played; at the end, we will add nil to say that is the end of the list.

At the end, we will call run the sequence action on hero.

Now, the hero will start at the initial position, but when he gets back, he will be blue in color.

The output of the code is as follows: