Animation being overwritten 1/2 way through being run

I’n my game when the player isn’t moving i’d like it to be in the idle animation but when moving be in the run animation. I achieved it through this code:

if self.velocity == 0 then
    play_animation(self, anim_idle)
else
    play_animation(self, anim_run)
end

But when the player hits an object i want it to go into the hurt animation, this is done in a different subroutine to the above code

play_animation(self, anim_idle)

But i found that eventhough it seems to be coded correctly the hurt animatin doesn’t run, i have changed the code so that the run animation code looks like this:

if self.velocity.x > 0 then 
    play_animation(self, anim_run)
end

this works fine as the player runs when moving and is hurt when collising with an object. But i was wondering if i could still impliment the idle animation after the hurt animation or if the player has stopped moving to make the game look smoother. Has this happened to anyone else?

if self.velocity == 0 then
   play_animation(self, anim_idle)
else
   play_animation(self, anim_run)
end

Think about what this part is doing. Every time this code is run, the current animation will always be set to either anim_idle or anim_run, completely ignoring anything else happening in the game. A quick solution is to add a flag like self.is_hurt and check for it when setting the current animation:

if self.is_hurt then
   play_animation(self, anim_hurt)
elseif self.velocity == 0 then
   play_animation(self, anim_idle)
else
   play_animation(self, anim_run)
end

Just keep in mind that this can get very messy in a bigger project with lots of different animations!

2 Likes

yeah it seems like a lot of variables to keep tack of. I tried a few different ways but just decided in the end to instead have a sound that plays when the player hits an enemy instead of an animation. Thanks for the help tho it’s really appreciated

1 Like