Animation End event is not working for a specific case

Zuljaras

Member
Hello!

So I have this problem that I create a lightning effect and after its animation is done I destroy it. However the instance is NOT destroyed if I have odd number of sprite images.

Here is the code:

GML:
spark1 = instance_create(self.x+20,self.y+25,logo_lightning_obj);
        spark1.sprite_index=logo_lightning_sparks_spr;
        spark1.image_speed=choose(0.2,0.3,0.4);
       
        spark2 = instance_create(self.x+200,self.y+34,logo_lightning_obj);
        spark2.sprite_index=logo_lightning_sparks_spr2;
        spark2.image_speed=choose(0.2,0.3,0.4);
       
        spark3 = instance_create(self.x+choose(140,170,200),self.y+choose(25,34,45),logo_lightning_obj);
        spark3.sprite_index=logo_lightning_sparks_spr;
        spark3.image_speed=choose(0.2,0.3,0.4);
       
        spark4 = instance_create(self.x+choose(100,130,200),self.y+choose(25,34),logo_lightning_obj);
        spark4.sprite_index=logo_lightning_sparks_spr2;
        spark4.image_speed=choose(0.2,0.3,0.4);
       
        spark5 = instance_create(self.x+choose(50,90,130),self.y+45,logo_lightning_obj);
        spark5.sprite_index=logo_lightning_sparks_spr;
        spark5.image_speed=choose(0.2,0.3,0.4);
And this is the code of the instances:
Create event:
Code:
image_index = 0;
Animation End event:
Code:
instance_destroy();

logo_lightning_sparks_spr2 - has 9 images
logo_lightning_sparks_spr - has 10 images

Instances with logo_lightning_sparks_spr2 are not destroyed on animation end.

The game is running at 60 FPS.

There has to be some explanation. Especially when you consider that it is working flawlessly on GMS1.4. I thought that Animation end event is executing ALWAYS when the Sprite animation ends. The ODD numbered sprites loop like crazy and the instance do not destroy itself.
Any ideas?
 

FoxyOfJungle

Kazan Games
Instead of using the Animation End event, do it in the Step Event.

GML:
if (image_index > image_number - 1)
{
    instance_destroy();
}
This seems to be a bug that is now available in version 2.3.1.
 

Zuljaras

Member
Instead of using the Animation End event, do it in the Step Event.

GML:
if (image_index > image_number - 1)
{
    instance_destroy();
}
This seems to be a bug that is now available in version 2.3.1.
Thank you. But really a bug? Is it reported?

I would love to use the Animation End event.
 

FoxyOfJungle

Kazan Games
I'm not sure if it is, but I saw some other users reporting the same problem...
Anyway, it is better to submit a ticket to YoYo Games support.
 

Slyddar

Member
if (image_index > image_number - 1) { instance_destroy(); }
This code will skip the last frame of the animation, or at least some of it. If you use the two macros below, you can be sure you are getting all of the frames of the last image_index.
Code:
//speed per frame
#macro FRAME_SPEED            (sprite_get_speed(sprite_index)*image_speed)/room_speed
//end of animation
#macro ANIMATION_END        image_index >= (image_number - FRAME_SPEED)
Then you can just check if ANIMATION_END {} where needed.

I've noticed since 2.3 the Animation End event does not always trigger as well. Not sure exactly why, but it's definitely a bug, so I just run this code in the step event for those objects affected.
 

Zuljaras

Member
This code will skip the last frame of the animation, or at least some of it. If you use the two macros below, you can be sure you are getting all of the frames of the last image_index.
Code:
//speed per frame
#macro FRAME_SPEED            (sprite_get_speed(sprite_index)*image_speed)/room_speed
//end of animation
#macro ANIMATION_END        image_index >= (image_number - FRAME_SPEED)
Then you can just check if ANIMATION_END {} where needed.

I've noticed since 2.3 the Animation End event does not always trigger as well. Not sure exactly why, but it's definitely a bug, so I just run this code in the step event for those objects affected.
I have reported the bug as it is something crucial. I would hate to think that I must go through all objects that use the Animation End event and alter it.

At least now I know that it is indeed a bug.
 

Slyddar

Member
if (image_index > image_number - 1) { instance_destroy(); }
I literally took this from the manual page, as I like to check that I'm giving the correct code, but I haven't tested it, though.
The problem with it is the last frame will only show the first time image_index is greater than the last image_index value. By that I mean if you have 4 frames, 0, 1, 2 and 3, and a frame speed of 12, image_index will increment 0.2 per step, as 12/60 (room_speed) is 0.2. So image_index will be 0, 0.2, 0.4 and so on... 2.4, 2.6, 2.8, 3, and then on the frame when it is 3.2, the condition is true and the instance_destroy() will run. We essentially miss seeing the majority of the animation of this final frame. It is dependent on the sprites image_speed though, as to how much you miss.
 
Top