• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

how to get a death animation to work ?

so i'm making a platform shooter and well i'm having trouble getting my enemy death animations to play i saw this video it was drag and drop and whle it got my enemies to die the animation didn't work.

so i decided to find one that was more technical and now when i try to run the game it just give me a compile failed error ,here's said video


also here is what i coded in that should have work but it's causing the error

so in object_e(which is what my enemy object is called) i have a collision event with obj_bullet and a execute code with this in it

var randomangle = random (360);
var o;
o = instance_create(x,y,object_edeath);
o = angle = 0 + randomangle;

with(object_edeath);
{
direction = angle;
image_angle= angle;
speed = .5;

}
instance_destroy();

hopefully someone can help figure out this problem i sure it's pretty simple.
 

Jezla

Member
Is the bullet actually colliding with your object so that the collision event triggers?

If so, I would simply either use instance_change(), or just change the sprite index to the death sprite. The, in the animation indecent, if the sprite index is the death sprite, destroy the instance.
 
Is the bullet actually colliding with your object so that the collision event triggers?

If so, I would simply either use instance_change(), or just change the sprite index to the death sprite. The, in the animation indecent, if the sprite index is the death sprite, destroy the instance.
yes i think the bullet is supposed to hit the enemy and then the death animation should play. btw do i just erase everything in the code and just put that or do i add on to the existing code ? lol just trying to make sure i'm understanding you completely
 

Jezla

Member
Erase what you have, it's too complicated for what you want to do. It's also full of errors, as you didn't copy the code from SS's tutorial correctly, which is why copying should be avoided in favor of figuring out how to do it independently. Tutorials should be a guide, not a source.

There are two options I would recommend:

1. If you choose to have a death object, then in the collision event of the enemy with the bullet, change the instance of the enemy to the death object using instance_change(). However, for the death object to be destroyed when it's animation plays, you have to put instance_destroy() in the death object's animation end event. Or -

2. Just have your enemy object, but make a sprite for the death animation (which I assume you already have), then in the collision event, change sprite_index to the death animation sprite. In the enemy's animation end event, check if the sprite_index is the death animation, and if it is, destroy the instance.

However: both of these hinge on whether or not the collision event is even triggering. If your bullet is moving too fast, it could be skipping right past the enemy object without ever colliding. Remember, a collision only occurs if the collision masks of the objects overlap. If the bullet is travelling 20 pixels/step, and the enemy is only 10 pixels wide (for example), the bullet may never collide with the enemy. It will be in front one step, and then be behind the next step. If that's what is happening, you will have to use more sophisticated collision detection or adjust your bullet speed.
 
Last edited:
Erase what you have, it's too complicated for what you want to do. It's also full of errors, as you didn't copy the code from SS's tutorial correctly, which is why copying should be avoided in favor of figuring out how to do it independently. Tutorials should be a guide, not a source.

There are two options I would recommend:

1. If you choose to have a death object, then in the collision event of the enemy with the bullet, change the instance of the enemy to the death object using instance_change(). However, for the death object to be destroyed when it's animation plays, you have to put instance_destroy() in the death object's animation end event. Or -

2. Just have your enemy object, but make a sprite for the death animation (which I assume you already have), then in the collision event, change sprite_index to the death animation sprite. In the enemy's animation end event, check if the sprite_index is the death animation, and if it is, destroy the instance.

However: both of these hinge on whether or not the collision event is even triggering. If your bullet is moving too fast, it could be skipping right past the enemy object without ever colliding. Remember, a collision only occurs if the collision masks of the objects overlap. If the bullet is travelling 20 pixels/step, and the enemy is only 10 pixels wide (for example), the bullet may never collide with the enemy. It will be in front one step, and then be behind the next step. If that's what is happening, you will have to use more sophisticated collision detection or adjust your bullet speed.
so i listened sort of but putting sprite_index in the code for some reason it always said it was a error so with collision event i just did a drag and drop and made the sprite change when the bullet hit the enemy but the animation kept repeating so i put a destroy instance in the end animation event but it always just destroyed the enemy from the beginning or did the whole repeat animation thing. so yeah some progress was made but still having some problems.

so in obj_e i have a collision event with a change spirte action. and a end animation event with this code in it
{
if (object_edeath)
instance_destroy();
}

I'm assuming something is wrong with this code because it either destroys the whole enemy or repeats the animation, so yeah got the animation working just need to make stop......lol baby steps
 

Jezla

Member
Okay...

Code:
//obj_e collision event with bullet
instance_change(obj_death, true); //Change the instance
with (other) instance_destroy; //Destroy the bullet

///In obj_death animation end event
instance_destroy();

Code:
///In obj_e collision event with bullet
with (other) instance_destroy();  //Destroy the bullet

sprite_index = spr_death;  //Change the sprite

///in obj_e animation end event:
//Check the sprite, and if it's the death animation, destroy the instance
if (sprite_index == spr_death) instance_destroy();

You'll want to change the names to match your object and sprite names, but that should work for you. Method 1 is probably the easiest, and is used in the 1945 scrolling shooter tutorial that comes with GM:S.

If you haven't done it, I really recommend going through the included tutorials, there's loads of information in them that will help you understand how GM:S and GML work.
 
Okay...

Code:
//obj_e collision event with bullet
instance_change(obj_death, true); //Change the instance
with (other) instance_destroy; //Destroy the bullet

///In obj_death animation end event
instance_destroy();

Code:
///In obj_e collision event with bullet
with (other) instance_destroy();  //Destroy the bullet

sprite_index = spr_death;  //Change the sprite

///in obj_e animation end event:
//Check the sprite, and if it's the death animation, destroy the instance
if (sprite_index == spr_death) instance_destroy();

You'll want to change the names to match your object and sprite names, but that should work for you. Method 1 is probably the easiest, and is used in the 1945 scrolling shooter tutorial that comes with GM:S.

If you haven't done it, I really recommend going through the included tutorials, there's loads of information in them that will help you understand how GM:S and GML work.
didn't work at first but i got the first method to work thanks for the help :)
 
Top