Book Image

Building Android Games with Cocos2d-x

By : Raydelto Hernandez
Book Image

Building Android Games with Cocos2d-x

By: Raydelto Hernandez

Overview of this book

Table of Contents (16 chapters)
Building Android Games with Cocos2d-x
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Animating sprites


In order to give a more professional aspect to our game, we could animate our sprites so that it does not constantly show a still image but rather displays animated characters, enemies, and obstacles. Cocos2d-x provides an easy mechanism to add these kinds of animations to our sprites, as we can appreciate in the following code listing:

//Animations
Vector<SpriteFrame*> frames;
Size playerSize = sprPlayer->getContentSize();
frames.pushBack(SpriteFrame::create("player.png", Rect(0, 0, playerSize.width, playerSize.height)));
frames.pushBack(SpriteFrame::create("player2.png", Rect(0, 0, playerSize.width, playerSize.height)));
auto animation = Animation::createWithSpriteFrames(frames,0.2f);
auto animate = Animate::create(animation);
sprPlayer->runAction(RepeatForever::create(animate));

Improving performance with sprite sheets

Although we can create sprite animations based on the images located in several files, as we have done in our previous code, it would be very...