cc.Animation Stop or Stop('name') Sometimes Doesn't Work

cc.Animation Stop or Stop('name') Sometimes Doesn't Work

In my project I have enemies that spawn, either die and are recycled, or go out of world bounds and are recycled. The enemies that die have an explosion animation that is played.

this.particle = this.node.getChildByName('explosion');
this.particleAnim = this.particle.getComponent(cc.Animation);'explosion');

Playing works just fine and I have an event that fires at the end of the animation: onAnimCompleted() – just like the example in the docs.

I have two ways to ensure the animation stops and goes back to the beginning frame. The onAnimCompleted event, which fires “disableEnemy()”, or right before the enemy is recycled which fires the enemies “enableEnemy()” function.

In both functions/methods; disableEnemy() and enableEnemy() I call:


This works and then randomly does not work. I’ve tried other ways to enforce the animation to go back to the first frame (basically invisible):

this.particleAnim.setCurrentTime(0, 'explosion');

I’ve tried all of these, in different combinations, and it’s the same situation every time: it works, and then randomly it just stops working. It sticks in a random keyframe (or possibly the last played keyframe before the node was inactive, but the stop() function is called before setting the node inactive).

These are all called before the node is set to inactive or active.

Does this take time to execute? At the end of my “disableEnemy()” function it looks like:

this.particleAnim.stop(); = false;

Or in the “enableEnemy()” function it would look like:

this.particleAnim.stop(); = true;

And it’s been any of the previously written combinations; with a name, without a name, also setting the animation time to zero, etc… Does the “stop()” function not execute when the node is inactive?

This animation is also not set to a looping wrap mode; it’s set to Normal.

I will randomly see my enemies come into view with half the explosion paused, or near the end of the animation paused, and lots of times it works as expected.

Any suggestions?


For now I have added enabling/disabling the animations sprite. This seems to have worked but since everything about this is so random I will have to keep an eye out while I do my normal game-play testing.

this.particleSprite = this.particle.getChildByName('explosion').getComponent(cc.Sprite);

When all should be disabled: this.particleSprite.enabled = false;
Right before I play the animation: this.particleSprite.enabled = true;

I hope this helps someone else out there. I’m not sure if it’s an issue with what I’m doing or how I created the sprite frame animation, etc., I am very new to Cocos.

Sometimes weird things may happen if a function called not correct phase of engine loop.

For example if you deactivate a node physics component then edit and reactivate at same frame it breaks the engine loop. Sometimes if you change a nodes parent that may break again.

Usually I use scheduleOnce to let engine to execute my function at correct time in engine loop like this:


This calls your function on before next frame.

Hope it helps friends👍