Animation stuck on Frame 0

B

buddhajuggles

Guest
I am having an issue getting an enemy to play his death animation before he is destroyed.

if(CurrentHp <= 0 && sprite_index != spr_CrowFloor)
{
sprite_index = spr_CrowFloor;

// if(image_index == 3)
// {instance_destroy();}

}
if i set the image_ index == 0 the code works , the issue is that my animation is not playing past frame 0.
I added bit about sprite_index_ != to spr_CrowFloor incase the animation event was re starting itself.
the issue is present without it. I have also tried adding an annimation end event which only works as
well if I only use 1 frame of my animation. It is particularly frustrating because all my other animations
play through just fine. Incase it is due to some of the other code this is his step coding in its entirety ,the above code is below this.

image_xscale = -1;
if(I💩💩💩💩 == true)
{
sprite_index = spr_EnemyHit1;
}


else {sprite_index = spr_enemy;}




if(OnGround == true)
{
GroundY = y;
}

depth = -1*GroundY;


thank you in advance.
 
Last edited by a moderator:

angelwire

Member
The line: image_index = spr_CrowFloor; is wrong, you should be setting it to 0 there. But if you're setting it to 0 while the health is at 0, it'll keep getting set to 0 every step. You need a variable to tell whether or not the enemy has started dying already. If it has started dying already then you shouldn't set the image_index to 0. Here's a fix:
Add this to the enemy object create event:
GML:
is_dying = false;
And then replace "image_index = spr_CrowFloor;" with:
GML:
if (is_dying == false)
{
    image_index = true;
    is_dying = true;
}
 
B

buddhajuggles

Guest
sorry about the miss type, thanks for noticing , It actually reads sprite_index = CrowFloor;
I tried something similar and the animation wont go past frame 0 .
I had made a death = false variable in create to correspond with a
if (death==true)
{instance_destroy()}
set an alarm to set death = true
and in step had
if(CurrentHp <= 0 && sprite_index != spr_CrowFloor)
{
sprite_index = spr_CrowFloor;
alarm[1] = 4;
}
not sure if that was a proper alarm use.
 
B

buddhajuggles

Guest
Where are you setting image_speed, if anywhere?



Why are you setting the image_index to a boolean? Please think before you type things.
image_speed is set in the create event
the enemy plays his hurt animation just fine which are called on the same step event

if(I**** == true)
{
sprite_index = spr_EnemyHit1;
}


else {sprite_index = spr_enemy;}

these animations are called the same way as the death animation I don't understand why it wont play
 

angelwire

Member
I don't think you'll need an alarm. You can do all the work through the image_index. A variable like is_dying should tell the enemy object whether or not to start the death animation from 0. If the death animation plays every step the enemy's health is at 0, then it will keep replaying and setting the image_index to 0.
I think instead of calling the variable "is_dying" you could call it "death_animation_is_playing". And then only set the image_index to 0 when "death_animation_is_playing" is false.
Here's the code for that:
GML:
//If the enemy is out of health and it's not playing the spr_CrowFloor sprite
if(CurrentHp <= 0 && sprite_index != spr_CrowFloor)
{
    //If it hasn't already started the death animation
    if (death_animation_is_playing == false)
    {
        //Start the death animation by changing the sprite_index
        sprite_index = spr_CrowFloor;
        //Set the image_index to 0
        image_index = 0;
        //Set the death_animation_is_playing to true so it doesn't restart next step
        death_animation_is_playing = true;
    }
    
    //Here you can add you instance_destroy()
    if (image_index == 3)
    {
        instance_destroy();
    }
}
 
Why not simply use the Animation End event and in there just check if the sprite_index is the one you use for the death animation - and if it is do the instance_destroy()? This is exactly how I have done it in the past, and it does not require any checks on the image_index or use of an alarm either.
That is what this event is for as it will trigger when the end of the animation has been reached. It saves trying to work out exactly what frame to check things on - especially as there are always chances that the image_index will never be exactly the number that you want.
 
B

buddhajuggles

Guest
Why not simply use the Animation End event and in there just check if the sprite_index is the one you use for the death animation - and if it is do the instance_destroy()? This is exactly how I have done it in the past, and it does not require any checks on the image_index or use of an alarm either.
That is what this event is for as it will trigger when the end of the animation has been reached. It saves trying to work out exactly what frame to check things on - especially as there are always chances that the image_index will never be exactly the number that you want.
You are right. I have done it this way and it works. My issue is the animation itself never playing past frame 0. If I cut my animation down to 0 the event works. I did that for test purposes. I cant find anything in my code to cause this frame 0 freeze.
 
B

buddhajuggles

Guest
I got it to work, rearranged the code I had so all the sprite events where together, and then set an animation end event for spr_floor to destroy the object. Though I am happy to have the animation working now I still don't understand why it wasn't playing all its frames before even if the code was fragmented.

if(I💩💩💩💩 == false && CurrentHp > 0)
{
sprite_index = spr_enemy;
}
else if(I💩💩💩💩 == true && CurrentHp > 0)
{
sprite_index = spr_EnemyHit1;
}

else if(CurrentHp <= 0 && sprite_index != spr_Floor)
{
sprite_index = spr_Floor;
}
Thank you all for the responses, just writing about it got me to think it over and
helped me a lot.
 
Top