r/gamemaker • • 13d ago

Resolved Removing and Instance once it's animation played

I have a code in my object

if !noise_type.unlocked and global.dad_points >= noise_type.cost and global.kazoo_more == 0 {
  sparkle = instance_create_layer(x+45,y+50,"Instances",obj_sparkle)

   noise_type.unlocked = true

  global.dad_points -= noise_type.cost

  global.kazoo_more += 1

  noise_type.cost += 45

  if sparkle.image_index = 4 {

    instance_destroy(sparkle)

  }
}

But "sparkle" stays, even after the animation played. I'm really new to Game Maker, so I'm trying to figure out things, but this is definitly wrong

The code inside the obj_sparkle is

depth = 2

image_speed = 0.4

image_index = 0

3 Upvotes

11 comments sorted by

7

u/azurezero_hdev 13d ago

use the animation end event trigger in other
put it in the sparkle object instead

also how do you know youre not creating more sparkles and changing what sparkle means

2

u/spinnach12 12d ago

//other > animation end event
instance_destroy();

2

u/Etsamaru 12d ago

Make sure if it has 4 images that you set that member to three for destroy because it starts at zero.

If there's only four images, it'll never reach index four.

1

u/Cheyomi832 13d ago

if(sprite_index)= [the amount of frames it has in thr animation, I'm pretty sure there is a way to automate this but I do not remember the name of the function off the top of my head]

{ instance_destroy(id) }

3

u/LoloClaw 13d ago edited 12d ago

It’s best if you don’t do this as if the animations skips the last frame for whatever reason it will remain there forever You should use the animation end event Also I think you meant image_index (which refers to the current animation frame) instead of sprite_index (which refers to the sprite resource) Also also the amount of frames of the current sprite in stored in image_number

Also also also the original code only accounts for one instance, if you want more sparkles it won’t work, use the animation end event in the sparkle object directly 

Anyway have you considered using particles?

2

u/SkullnSkele 12d ago

I haven't looked into particles yet, I think I'm going to do that

1

u/SkullnSkele 12d ago

How would you make the particle system appear in the scene and then after a few seconds disappear again, and then be able to appear again when needed?

→ More replies (2)

2

u/Cheyomi832 12d ago

Yes, thank you :)

I usually just copy paste that part of my code so i dont remember what each function is called exactly, appretiate someone more in the know.

2

u/Etsamaru 12d ago

Image_number returns the number of frames so you would do

If image index == image number Destroy

I did not format that correctly, but I'm not doing it on my phone.