O'Reilly logo

Learning Cocos2d-x Game Development 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

Coding the player animation

Now that we have the files ready to animate, let's first create a simple animation loop. Later, we will see how to go from one animation to another depending upon the player state.

First, let's just play the idle animation.

In the HelloWorldScene.cpp file, right after you added the player to the display list, type the following code:

 //player animation CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("player_anim.png"); CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("player_anim.plist"); hero->createWithSpriteFrameName("player_idle_1.png"); hero->addChild(spritebatch); //idle animation CCArray* animFrames = CCArray::createWithCapacity(4); char str1[100] ...

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