Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

Table of Contents (19 chapters)
Learning Cocos2d-x Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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] = {0};
    for(int i = 1; i <= 4; i++)
    {
        sprintf(str1, "player_idle_%d.png", i);
        CCSpriteFrame* frame = cache->spriteFrameByName( str1 );
     ...