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);
this.particleAnim.play('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.particleAnim.stop('explosion');
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.stop('explosion');
this.particleAnim.stop();
this.particleAnim.setCurrentTime(0);
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();
this.node.active = false;
Or in the “enableEnemy()” function it would look like:
this.particleAnim.stop();
this.node.active = 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?
Thanks!