• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How can I create a sprite after destroying an object?

O

Oxiriark

Guest
I am new to GML.
I would like to know if I can create a sprite animation from destroying an object.
Right now I have a collision check on the player object with this code
Code:
// check coin collect
var _instance = instance_place(x, y, instance_nearest(x, y, obj_coin));
if (_instance != noone)
{
    instance_destroy(_instance, false);
    audio_play_sound(snd_coinGrab, 0, false);
}
I have an effect in the form of a sprite right now, it is 11 frames long and I want it to create it when the coin is destroyed.
What is the most effective way to do this?
 
G

Guest User

Guest
The fastest method to do this is to make a new variable in the draw event, for example:
Code:
collected=0;
The 'collected' variable will be used to see if the coin has been picked up already. Instead of having the instance immediately destroyed, place this code in the place of instance_destroy:
Code:
if (_instance != noone)
{
   collected=1;
   mask_index=-1;
   sprite_index=spr_coin_pickup_animation;
   image_speed=0.4;
   audio_play_sound(snd_coinGrab, 0, false);
}
And in a new event, the 'Animation End' event, just simply add this:
Code:
if(collected)
{
   instance_destroy();
}
 
Last edited by a moderator:

Yal

🐧 *penguin noises*
GMC Elder
I don't think having the object control a destruction effect is the best approach, you will need to duplicate code for EVERY object you destroy this way.

It's better to have a single "animation effect" object, which has no sprite by default, which destroys itself in its Other-->Animation End event. To spawn an effect when something is destroyed: create an instance of this object, give it the correct sprite. 2 lines of code, and potentially every destructible object in your game can use the same logic.
 
P

ParodyKnaveBob

Guest
Howdy, Oxirlark, and welcome to GM and the GMC! $:^ ]

Over time, I've tried to get away from the method of creating an "animation object" instance in favor of particles using a part_type_sprite() definition. Then, on Destroy Event, just fire off the particle type you defined at the beginning of the game. "Set it, forget it, let it all disappear." $;^ b

I hope this helps,
 

Yal

🐧 *penguin noises*
GMC Elder
You've got much less control over particles, I try to shy away from them unless the effect I wanna create require hundreds of individual entities to look good. Debris getting launched in the direction something was destroyed and bouncing on the terrain a few times before disappearing, for instance, is a lot easier to do with objects.
 
Top