O'Reilly logo

Building Android Games with Cocos2d-x by Raydelto Hernandez

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

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); ...

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