Destroy object after animation

Hello. I am trying to play and animation and then destroy the object, however, I can either have the object being destroyed before the animation is noticed, or the animation happening but the object not destroyed. Hoping someone might be able to help with this. The code I am trying to use currently is

BulletEnemy(bullet, enemy)
    {     

        enemy.play('destroyEnemy', false)
        enemy.once('animationcomplete', ()=> 
        {
            console.log("anmiation complete");
            enemy.destroy();
        }
        )
}

What happens with that code?

with the code above, it will keep playing the animation, and not destroy the object, even after animation complete

If this is in a collide callback then you should disable or destroy bullet so the collision doesn’t continue.

Use enemy.play('destroyEnemy').

the code is in a function that will be called from a physics.add.collider.

I have tried the following. but the enemy zombies do not become destroyed, It only repeats animation. And the console.log string does not print out either.

BulletEnemy(bullet, enemy)
{

    enemy.play('destroyEnemy')
    enemy.once('animationcomplete', ()=>  {
        console.log("anmiation complete");
        enemy.destroy();
    })

}

Can you check that bullet and enemy are what you think they are?

console.log('bullet?', bullet);
console.log('enemy?', enemy);

And use enemy.play('destroyEnemy', true).

yes they are what I think they are. I checked with the code, and also tested by destroying each one. adding true in still did not help with the situation

Nothing should prevent enemy.destroy() from happening.

Are you creating or moving a second enemy somehow?

I do not believe so, when I used breakpoints in the browser, the code goes from enemy.play → enemy.once and then skips the rest of the code, so no console call or enemy.destroy

The callback won’t happen until the animation completes.

If you destroy the enemy before that, the animation won’t complete either.

I have got it working now. Not sure what the issue was. I redone the code for the animations and it is now working. Thank you for taking time to try and help me.