How to make an animation play and finish before the next line of code is executed

I want to create a way for my player to die in the game, when they reach 0 health, I want them to have an animation play (And either repeat a set number of times or play for a set amount of time) and then the program to close (for now, until i make a menu) but I don’t know how to do that. This is the code I have at the moment, and it just closes the program as soon as the player reaches 0 health.
if playerHealth == 0 then".", “release_input_focus”)
play_animation(self, hash(“Death”))

I assume you are talking about a flipbook animation?
It has a nice callback function when the animation has completed.

sprite.play_flipbook("#sprite", "death", function() os.exit() end)

I don’t use the flipbook animation stuff, I just use play_animation which works fine

sure it works fine, but as you said u need more control over the animation so you will need to look elsewhere. And thats why defold has something like flipbook.
play_animation is just for playing the animation, basically fire and forget :wink:

ok im honest here, until now i didn’t even know there is something like play_animation() in defold. I assume its just a sugary way for"#sprite", "play_animation",...) ?
if so, than you could also use on_message for checking the completion of the animation.

But imo thats kind of an ugly way to handle this.

1 Like

found this in docs API reference (sprite)

I’m guessing play_animation here is a function you’ve defined that looks something like this:

local function play_animation(self, animation)
    if animation ~= self.current_animation then
        sprite.play_flipbook("#sprite", animation)
        self.current_animation = animation

It’s important to note that this code doesn’t stop to let the animation finish, so your call to os.exit() will always run immediately. Luckily sprite.play_flipbook() can also take a function to run when the animation completes. Adding that to your code:

local function play_animation(self, animation, complete_function)
    if animation ~= self.current_animation then
        sprite.play_flipbook("#sprite", animation, complete_function)
        self.current_animation = animation
if playerHealth == 0 then".", "release_input_focus")
    play_animation(self, hash("Death"), function()

Now os.exit() will run when the Death animation finishes.

Nevermind, I’ve figured out how to get flipbook to work, i’ve just been typing it wrong.
How do I get it to play a certain number of times though?
I want the animation to play 3 times before it finishes