Game crashes when running animation

Hi.

I added coin system to my horizontal runner game. I can collect coins without animation, everything is ok but… I want to show an effect when a coin is collected.

So I made this sprite sheet which has 3 textures (collected.png):

collected

I tried adding animation to my game but it crashes when I collect a coin. The animation doesn’t run. Let me copy a few parts of my code here.

Below code is inside “HelloWorldScene.cpp” and it calls after all textures loaded in loading screen.

SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("collected.plist");

Vector<SpriteFrame*> animFrames(3);

char str[100] = {0};
for(int i = 1; i < 4; i++)
{
    sprintf(str, "collected%02d.png", i);
    SpriteFrame* frame = cache->getSpriteFrameByName(str);
    animFrames.pushBack(frame);
}

anim = Animation::createWithSpriteFrames(animFrames, 0.3f);

When I collect a coin, below code runs.

...
coinsCollected++;
group->removeCoin(jk, coinValue);
setCoinsCollectedText();

And here is removeCoin function:

void CoinGroup::removeCoin(int index, int value) {
    ...
    this->getChildByTag(index)->setVisible(false);

    auto spriteBatch = SpriteBatchNode::createWithTexture(texBatch);
    Sprite* sprAnim = Sprite::createWithSpriteFrameName("collected01.png");
    sprAnim->setPosition(this->getChildByTag(index)->getPosition());
    spriteBatch->addChild(sprAnim);
    this->addChild(spriteBatch);
    sprAnim->runAction(Animate::create(animation->clone()));
}

animation variable is passed from main class, when initializing my CoinGroup object.

I hope I could tell my problem. It’s complex because I have to use an extra class (CoinGroup).
Please help thanks.

This seems like a scope issue.

Take a look here: http://cocos2d-x.org/docs/programmers-guide/actions/index.html

Can you give more details about “scope issue”

I looked at the article but couldn’t find anything.

what I mean is some object or variable probably goes out of scope.

You are not passing in animation into CoinGroup::removeCoin, how come?

I’m not passing animation into CoinGroup::removeCoin but I store it in the memory, when I initialize the CoinGroup.

Like this:

bool CoinGroup::init(std::string strPattern, Texture2D *texture1, Texture2D *texture2, Texture2D *texture3, Animation* anim, Texture2D* sprBatch)
{
...
    texBatch = sprBatch;
    animation = anim;
...
}

place a break point and make sure you don’t see any weird gibberish here or anything? Does the assignment actually work?

Yes the assignment works. By the way my game is not crashing, it freezes after my ball collides with a coin.

oh you say game crashes in the title of this thread. :slight_smile:

So if is it freezing, are you testing on actual hardware or in an emulator? If an emulator, please try on hardware. Emulators are not always representative of how your game will run on devices.

I’m testing on the phone (S6 Edge). My computer can’t run x86 emulator (it says intel haxm bla bla bla) and runs ARM emulator very slow. So I’m testing on phone.

When I disable this line in CoinGroup::removeCoin function, the game keeps running but the animation doesn’t play, only first frame appears:

sprAnim->runAction(Animate::create(animation));

I think it’s a problem with animation variable. Do I have to retain the animation or something?

try passing in anim into removeCoin and using it in place of animation.

Is there any output on the console?

Now i tried passing into removeCoin function but it didn’t work.

It crashes like in this video:

I checked debugger console but couldn’t find any error. If you mean Logcat i’ll check it tonight when I go home.

Would you want to DM me your project and I can debug it this weekend? Google Drive or something?

yes, but “proj.android_studio” folder is 1.7 GB and I think it takes too much time to upload it. proj.android-studio\app\build\intermediates\ndkBuild\debug this path contains 1.5GB. i don’t know why it’s too much. which folders should i upload?

classes/ and resources/ are a good start. I can use cocos new ... to drop them into a new project. Send them privately if you want.

Ok I solved the problem. There was a problem with animFrames. Thanks

What was the problem?

I deleted this line from main class (HelloWorldScene.cpp)

Vector<SpriteFrame*> animFrames(3);

I forgot that it’s already declared in HelloWorldScene.h. Now it works as it’s supposed to: