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