Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new to Cocos2d-x and looking for a good way/tutorial on how to play animations in Cocos2d-x.

share|improve this question
up vote 9 down vote accepted

Sprite animation is pretty simple. You just create a CCAnimation node, add the images to loop, then create an action using CCAnimate::actionWithDuration(float, CCAnimation, bool) and make the sprite run it.


CCAnimation * anim = CCAnimation::animation();
// There are other several ways of storing + adding frames, 
// this is the most basic using one image per frame.

CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true); 
// Duration, animation action and bool to return to frame 1 after finishing.

CCSprite *bear = CCSprite::spriteWithFile("bear1.png");
addChild(bear,0); //Don't forget to add any sprite you use as a child to the CCLayer!
share|improve this answer
Thanks but what is HelloWorld::getPlayer() ? I'm getting a crash on iPhone simulator on adding runAction(laanim); to my code. – 2600th Mar 14 '12 at 14:40
You can use a sprite or any other node you want, I have a method that returns a static sprite called _player that I have previously initialized. – MLProgrammer-CiM Mar 14 '12 at 14:44
Thanks it worked like charm ! :D – 2600th Mar 14 '12 at 14:47
I edited it for clarity now :) You are welcome. – MLProgrammer-CiM Mar 14 '12 at 14:48
CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true); doesn't work with the current version of cocos2d-x. What has to be changed? – Ben Aug 8 '12 at 18:57

In new version of CoCos2dx (2.1.1) you can use (it's working)

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();

CCSprite* sprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("slice2_0_0.png"));

CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("numbers.png");

CCArray* animFrames = CCArray::createWithCapacity(10);

char str[100] = {0};
for(int i = 0; i < 10; ++i)
    sprintf(str, "slice2_0_%d.png", i);
    CCSpriteFrame* frame = cache->spriteFrameByName( str );
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames,1.f);
sprite->runAction(CCAnimate::create(animation) );
share|improve this answer
There's an edit to this question in the edit review queue which renames spriteWithSpriteFrame to createWithSpriteFrame. I don't know enough Cocos2D to tell if it is an improvement. Would the edit improve this answer? – Anko Mar 15 '13 at 13:01

If you don't want to use a .plist file and want to continue with the Ef Es's answer with the current version of cocos2d-x, just change some lines as below:

    CCSprite * sprite  = CCSprite::create("bear1.png"); // NEW - create a sprite here
    CCAnimation * anim = CCAnimation::animation();
    // There are other several ways of storing + adding frames, 
    // this is the most basic using one image per frame.

    anim->setLoops(-1); // for infinit loop animation
    anim->setDelayPerUnit(0.1f); //Duration per frame
    //CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true); // this wont work in newer version..

    sprite->runAction(CCAnimate::create(anim) );
    sprite->setPosition(ccp(200,200)); //set position of sprite in some visible area

    this->addChild(sprite, 1); // cross check the Z index = 1 with your code

I think this can be the solution for Ben's question also.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.