• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 rookie asks for help

netoxinaa

Member
hellow there, i want to program something simple but i cannot find a decent way to do it (or i havent came up with one yet), i´m creating a space shooter and i want the enemy to be destroyed as the bullet collisions with it, AND THEN play the dead sprite for the enemy (it´s an explosion lol), it happens me either the dead sprite enters a loop where it dont stop, or the enemy gets destroyed but without playing the dead sprite. Thanks for your help!
 
Hi. Off the top of my head I can't remember if changing the sprite in the destroy event of the enemy would work, as I don't think it would actually play it before being destroyed.

What I would do is: either create an explosion object in the destroy event, or use instance change in the destroy event. Not really sure which is better - creating an instance might have a slightly larger cost to performance, whereas instance change requires some thought so as to change the original destroyed objects properties.

Whichever one you go with: the "new" object has its sprite set to be the explosion sprite, and under "animation end" you just put

instance_destroy()

It will destroy itself when the explosion animation has finished
 
Are you using the "drag and drop" functions or writing GML code?

Either way, here is a solution to your problem:
  1. Destroy the bullet
  2. Change the enemy object's sprite
  3. Once the sprite plays through all sub-images, destroy the enemy object

If you're using GML code, this should do the trick. I copied this example from the documentation, which should be your new best friend. It's a treasure trove of valuable information. Press the F1 key. ;)

Example:
Code:
if image_speed > 0
    {
    if image_index > image_number - 1 instance_destroy();
    }
The above code checks to see if the sprite is animating, and if it is it then checks to see if the current image_index is greater than the number of sub-images and if it is it destroys the instance.
I'm getting old, and forgot how "drag and drop" works... but the theory is the same. If you still can't figure it out, and you don't get better help from another member: Just send me a message, or bump this thread and I'll get you squared away.

One last thing: Congratulations on creating your own video game. Don't get discouraged, and stick with it. We're always here when you get yourself into a pickle. :cool:
 
Last edited:
Another way would be to create a "dead enemy" object and simply create an instance of that whenever you destroy an enemy. The "dead enemy" object should play through the destruction animation and then destroy itself in the Animation End event.
 

netoxinaa

Member
Are you using the "drag and drop" functions or writing GML code?

Either way, here is a solution to your problem:
  1. Destroy the bullet
  2. Change the enemy object's sprite
  3. Once the sprite plays through all sub-images, destroy the enemy object

If you're using GML code, this should do the trick. I copied this example from the documentation, which should be your new best friend. It's a treasure trove of valuable information. Press the F1 key. ;)

Example:
Code:
if image_speed > 0
    {
    if image_index > image_number - 1 instance_destroy();
    }
The above code checks to see if the sprite is animating, and if it is it then checks to see if the current image_index is greater than the number of sub-images and if it is it destroys the instance.
I'm getting old, and forgot how "drag and drop" works... but the theory is the same. If you still can't figure it out, and you don't get better help from another member: Just send me a message, or bump this thread and I'll get you squared away.

One last thing: Congratulations on creating your own video game. Don't get discouraged, and stick with it. We're always here when you get yourself into a pickle. :cool:

Thanks for the help i appreciate it man,
i implemented the code but for some reason the sprite keeps playing and playing,
maybe its something about my code im not doing correct? here it is (the collision with the bullet event):

Code:
instance_destroy(other);

sprite_index = spr_explosion;
image_speed = 1;

if image_speed > 0
    {
    if image_index > image_number - 1  instance_destroy();
 }
 

TsukaYuriko

Q&A Spawn Camper
Forum Staff
Moderator
image_index wraps back around to 0 after reaching the final sub image.

Use the Animation End to detect whether the animation has finished.

Also note that events do not block code execution. If this code runs only once in a Collision event, it will not run anymore once the condition would have been reached. Collision code runs starting anew for every frame there is a collision.

You should either use a boolean variable to signal that the instance is exploding (and thus no longer reacts to collisions), use a different object for the explosion or use a particle effect for the explosion instead of an instance.
 
This is not good advice. Take it a grain of salt.

image_index wraps back around to 0 after reaching the final sub image.

Use the Animation End to detect whether the animation has finished.

Also note that events do not block code execution. If this code runs only once in a Collision event, it will not run anymore once the condition would have been reached. Collision code runs starting anew for every frame there is a collision.

You should either use a boolean variable to signal that the instance is exploding (and thus no longer reacts to collisions), use a different object for the explosion or use a particle effect for the explosion instead of an instance.
TsukaYuriko is right. Did that make sense to you, Netoxinaa? If all of this is clear as mud to you: I'd recommend using a different object for the explosion. Use the bullet to destroy the enemy, and in the enemy's destroy event you should create an explosion object. For the sake of this example, I will assume the explosion has 8 sub-images.

For the explosion object, put this in your Create Event:

Code:
image_index = 0:
image_speed = 3;
Three is a number I picked at random. Adjusting this will control how fast the explosion plays.

And put this in the explosion's Step Event:
Code:
if image_index = 7
   {
   instance_destroy();
 }
In programming numbers will almost always start at zero, so the seventh image_index is actually the eighth sub-image (the final image in my imaginary explosion sprite.)

While this solution is far from optimal: It's the easiest to understand, and should get you past this roadblock. At some point, consider trying to accomplish this task in several different ways. It'll help you understand GML better, and make your next game even easier.

Good luck. Have fun. Wear sunscreen.
 
Last edited:
The Step Event thing that SilentxxBunny suggested is the wrong way to do it. Comparing image_index to a flat value is very bad form. Depending on your image_speed, it's entirely possible to skip the number you are hoping to hit (i.e. your image_index could easily go from 6.3 to 7.5 or something like that) thus never triggering the if conditional. You should instead follow TsukaYuriko's suggestion of using the Animation End event, which was built for this purpose entirely (i.e. do something when an animation is finished).
 
The Step Event thing that SilentxxBunny suggested is the wrong way to do it. Comparing image_index to a flat value is very bad form. Depending on your image_speed, it's entirely possible to skip the number you are hoping to hit (i.e. your image_index could easily go from 6.3 to 7.5 or something like that) thus never triggering the if conditional. You should instead follow TsukaYuriko's suggestion of using the Animation End event, which was built for this purpose entirely (i.e. do something when an animation is finished).
Awh, phooey. You beat me to the punch. I was thinking about this while doing dishes and realized that I misguided you. A more functional duct-tape solution would be this:

Create Event
Code:
image_index = 0;
image_speed = 0;
Step Event
Code:
image_index += 1;
if image_index = 7 {
instance_destroy();
}

RefresherTowel is right, though. If you understood TsukaYuriko's advice, definitely follow that.

I'm sorry for this brief moment of senility..A mind is a terrible thing to lose.

Edit: Alternatively, you could try this (step event) in conjunction with Animation End:
Code:
image_index += 1;

There's a thousand solutions to every problem. Some solutions are better than others, but don't let 1 stop you from trying the other 999. You might learn something in the process.
 
Last edited:

Bentley

Member
hellow there, i want to program something simple but i cannot find a decent way to do it (or i havent came up with one yet), i´m creating a space shooter and i want the enemy to be destroyed as the bullet collisions with it, AND THEN play the dead sprite for the enemy (it´s an explosion lol), it happens me either the dead sprite enters a loop where it dont stop, or the enemy gets destroyed but without playing the dead sprite. Thanks for your help!
Edit: I thought the dead sprite was a sprite based off the enemy. Since the dead sprite is an explosion, I'd take the advice above and create a separate instance. So ignore my post.

When a collision happens, you could check that the enemy's sprite is not spr_dead, and then set it to spr_dead. That way the code you want to run will only run once. For example:

obj_enemy
Collision event with bullet
Code:
if (sprite_index != spr_enemy_dead)
{
    sprite_index = spr_enemy_dead;
    image_index = 0;
    image_speed = 1;
    // And anything else you want to happen
}
Animation end
Code:
if (sprite_index == spr_enemy_dead)
{
    instance_destroy();   
}
 
Top