1
\$\begingroup\$

I'm trying to create an animation using a sprite sheet in Cocos2D-x. I'm doing the following. The game doesn't crash but the screen is still blank.

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto cacher = SpriteFrameCache::getInstance();
    cacher->addSpriteFramesWithFile("BatmanRun-hd.plist");

    Sprite* someSprite = Sprite::create();

    // load all the animation frames into an array
    Vector<SpriteFrame*> frames;
    for (int i = 2; i <= 10; i++)
    {
        stringstream ss;
        ss << "run_" << i << ".png";
        frames.pushBack(cacher->getSpriteFrameByName(ss.str()));
    }

    // play the animation
    Animation* anim = Animation::createWithSpriteFrames(frames, 0.05f);
    someSprite->runAction(Animate::create(anim));
    someSprite->setPosition(100, 100);

    this->addChild(someSprite);

    return true;
}

Is there something I'm missing? Thanks.

\$\endgroup\$
1
\$\begingroup\$

Start the sprite with the first frame:

auto someSprite = Sprite::createWithSpriteFrameName("run_1.png");

Pay attention to the code you use for creating the animation: You are adding frames 2-10. That's ok if it's a single shot animation.

I assume that you want to repeat the animation - just from the name "run". You'll have to add frames 1-10 in the for loop.

someSprite->runAction(RepeatForever::create(Animate::create(anim)));
\$\endgroup\$
0
\$\begingroup\$

don't create a empty sprite node. when you do Sprite::create(), load the first frame as the parameter. That is used to init contentSize and bounding-box variable of the sprite. So, that might help.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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