GML Code Equivalent for the "End Animation" Event?

Discussion in 'Programming' started by Anomaly, Sep 19, 2017.

  1. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    Just as there's many parallels for events, like "meeting_place" = the collision event..

    Is there code for end animation so I don't have to use another object and just use the sprite in my original object?
     
  2. Ragarnak

    Ragarnak Not Ragnarök Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    948
    Something like:
    Code:
    if (image_index > image_number - 1) {
        // Do something
    }
    EDIT: Might not be the ideal way if the image speed's small. See post #4.
     
    Last edited: Sep 19, 2017
    maratae likes this.
  3. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    ok i've been using exactly that..

    but hadn't set a new conditoinal in that "do something" in order to detour the remainder of code.
    thanks for reminding me to look at that.
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,716
    An animation ends when its upcoming animation frame will roll over back to 0:
    Code:
    if (image_index+image_speed >= image_number) {
      //...
    }
    This would trigger several times instead of once when the image speed is small, try and see for yourself.
     
    Zabicka, Justice, Bentley and 5 others like this.
  5. Rucksack

    Rucksack Member

    Joined:
    Oct 18, 2016
    Posts:
    3
    That does not work when you change the animation speed in the image editor. Then you have to use something like this:
    Code:
    (image_index+image_speed*sprite_get_speed(sprite_index) >= image_number)
    
     
    Zabicka, Pfap and Jenna like this.
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    WTF is this in GMS2? Why the hell would they force the Sprite animation speed in the Sprite itself? That's a horrible idea if that is what they did.
     
    MirthCastle likes this.
  7. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    845
    With GMS2, sprites now have a 'base' animation speed that you can set in the sprite itself (note: this effects all instance of that sprite in use). image_speed is now a multiplier of that base animation speed. If a sprite plays at 15fps, then setting image_speed to 2.0 will cause it to play at 30fps, and so forth.

    There was something about the simplicity to the way GMS1.4 handle animation speeds that I liked, but there's value in the way GMS2 handles them too. I haven't decided which way I like more. -edit- Of course, the old image_speed system isn't really 'gone' either. It's nothing more than setting the image_speed value to 0, and incrementing image_index by whatever value is appropriate for the playback speed you want.
     
    Bentley likes this.
  8. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,044
    That sounds like garbage.
     
    Zek, Fixer90, elsi11 and 2 others like this.
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    I would guess the reasoning was something along the lines of, "nobody ever uses deferring animation speeds between instances that use the same Sprite except in a situation such as adjusting the animation speed based on the movement speed of the instance, so we should just make it scale with image_speed."
     
  10. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    845
    Perhaps, but it's not as if the old method is gone. It just takes a couple lines of extra code to recreate.
     
  11. Roman P.

    Roman P. Member

    Joined:
    Mar 1, 2017
    Posts:
    211
    it is much handier.

    don't think i've ever had to type image_speed in gms2, and if i'd need to there's still an option.
     
  12. kupo15

    kupo15 Member

    Joined:
    Jun 20, 2016
    Posts:
    713
    Wow thanks for pointing this out. Another reason to not use image_speed built in but I can see it being handy in some cases.
     
  13. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    Seems like it would have more overhead. Instead of just adding to the image_index, doesn't GM now have to check the speed type first?

    But i see the usefulness after reading the help file on the change.
     
    elsi11 likes this.
  14. elsi11

    elsi11 Member

    Joined:
    Jul 18, 2017
    Posts:
    196
    I'm using an animation as a trigger for something. Don't know if stuff can go wrong by using sprites for major events. But what I intend to do is: change the sprite index and the image index. Then, I'd wait for the animation end with
    Code:
    if (image_index == image_number)
    At that point, I should be at the last frame, and ready for the major event. I thought about setting the image_speed to 0 so the animation stays at the last frame if it so happens that stuff doesn't get destroyed in time, but then I thought I'd rather not mess with image speed at all since I depend on the images rolling (saves me 1 line xD). Can I set the sprite_index to -1 or something so that I just erase the animation and it doesn't loop in case the object doesn't get destroyed in time? The easiest example is an explosion or death animation where you want to run the animation once and then that's it.

    So my question, basically is whether something like this is good:
    Code:
    if (image_index == image_number)
    {
        sprite_index = -1; //animation is over. Maybe we could put "image_index = image_number" as some kind of check mate infinite loop holding us at the last frame long enough for the other thing to run its course. I don't want to mess with speed.
        scr_super_major_event();
    }
    I'm on 1.4.
    What do?
    Also, is it customary to comment on ancient posts? This is the only forum I ever visited so I don't know the rules :)
     
  15. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,044
    The examples above should work for you, don't be afraid to set image_speed in GMS 1.4.
    Code:
    if (image_index + image_speed >= image_number)
    {
        image_speed = 0;
    }
    
    I do this too in my game and it works great.
     
    elsi11 likes this.
  16. kupo15

    kupo15 Member

    Joined:
    Jun 20, 2016
    Posts:
    713
    It's fine to use animation end as a trigger for major events. I do it everywhere in my game. Just make sure to do what Simon said and make that check

    >= image_number. If your image_speed isn't a clean number to land exactly on a whole it won't trigger (ie 4.15)

    Code:
    if >= image_number
    {
    image_speed = 0;
    image_index = image_number-1; // need to set it to last frame to hold
    }
     
    elsi11 and CMAllen like this.
  17. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    282
    I have upgraded Heartbeast solution for checking animation end.
    Heartbeast checked if it's last image in sprite where I've adjusted so it doesn't cut short last image

    Code:
    ///@description check if it's animation last step
    var frame = image_index;
    var frame_range = image_speed; //GMS2: var frame_range = image_speed * sprite_get_speed(sprite_index) / game_get_speed(gamespeed_fps);
    
    return image_index >= image_number - frame_range && image_index < image_number;
    
     
    Flaick likes this.
  18. Jenna

    Jenna Member

    Joined:
    Jan 31, 2018
    Posts:
    3
    Yay! This is exactly what I was looking for when I came here! I'd like to point out, though, that you need to have the sprite speed in your editor set to "frames per game frame" for this code to work correctly. By default it is set to "frames per second", so this was confusing me at first.
     
  19. Flaick

    Flaick Member

    Joined:
    Feb 3, 2019
    Posts:
    16
    Thanks! You saved my life!
     
    NeZvers likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice