Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

3 Answers 3

up vote 8 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.

Code snippet:

CCAnimation * anim = CCAnimation::animation();
// There are other several ways of storing + adding frames, 
// this is the most basic using one image per frame.
anim->addFrameWithFileName("bear1.png");
anim->addFrameWithFileName("bear2.png");
anim->addFrameWithFileName("bear3.png");
anim->addFrameWithFileName("bear4.png");
anim->addFrameWithFileName("bear5.png");
anim->addFrameWithFileName("bear6.png");
anim->addFrameWithFileName("bear7.png");
anim->addFrameWithFileName("bear8.png");

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!
bear->runAction(theAnim);   
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. –  Ef Es 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. –  Ef Es 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();
cache->addSpriteFramesWithFile("numbers.plist","numbers.png");

CCSprite* sprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("slice2_0_0.png"));
sprite->setPosition(ccp(GameScene::windowSize.width/2,GameScene::windowSize.height/3));

CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("numbers.png");
spriteBatchNode->addChild(sprite);
addChild(spriteBatchNode);

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 );
    animFrames->addObject(frame);
}
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->addSpriteFrameWithFileName("bear1.png");
    anim->addSpriteFrameWithFileName("bear2.png");
    anim->addSpriteFrameWithFileName("bear3.png");
    anim->addSpriteFrameWithFileName("bear4.png");
    anim->addSpriteFrameWithFileName("bear5.png");
    anim->addSpriteFrameWithFileName("bear6.png");
    anim->addSpriteFrameWithFileName("bear7.png");
    anim->addSpriteFrameWithFileName("bear8.png");

    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

 
discard

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.