Creating animation using sprite sheets

I am trying to create an animation but when I run the application I face this error:

libc(2406): Fatal signal 11 (SIGSEGV) at 0x00000014 (code=1), thread 2420 (Thread-144)

this is he code :

Sprite *s;
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites.plist");
    auto testBatchNode = SpriteBatchNode::create("sprites.png");
    this->addChild(testBatchNode);

    s = (Sprite *)Sprite::createWithSpriteFrameName("slice01.png");
    s->setPosition(ccp(Utils::s().width / 2, Utils::s().height / 2));
    s->setScale(Utils::getScale());
    
    s->stopAllActions();
    auto animation = CCAnimation::create();
    for (int i = 1; i <= 8; i++){
        char szName[100] = { 0 };
        sprintf(szName, "slice%01d.png", i);
        CCLOG("%s",szName);
        animation->addSpriteFrame(CCSpriteFrameCache::getInstance()->spriteFrameByName(szName));
    }
    animation->setDelayPerUnit(0.1f);
    s->runAction(CCAnimate::create(animation));
    testBatchNode->addChild(s);

and here is my Class.

I dont know why, but the problem comes out of “s->runAction(CCAnimate::create(animation));”
when I comment it out, I dont get any errors and obviously no animations too!

Am I creating the animation right?
should the animation be on an specific layer ?

It looks like you are mixing v2 and v3 a bit. This may be fine,.

I would stick a bit more closer to: http://www.cocos2d-x.org/wiki/Sprite_Sheet_Animation

and see if that helps.

1 Like

thank you, I used v3.0 and I have no problem now.

Just as an addition:

animation->addSpriteFrame(CCSpriteFrameCache::getInstance()->spriteFrameByName(szName));

May spriteFrameByName returned some null pointer. You should check the pointer first, instead of using the return value directly. So you can make sure it worked.

1 Like