1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 Changing Sprites: How do I set an animation?

Discussion in 'Programming' started by Dr_Nomz, Dec 3, 2019.

  1. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    619
    I want to have a normal sprite on a character for most of the time, until they start an attack. Once they do, their sprite changes to a different sprite (attack animation) and it'll stay that way until the sprite animation is over.

    I also want to keep this all in the DRAW event if possible. How do I this?
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    if image_index + image_speed >= image_number {
    //go back to idle or whatever if attacking
    //or set image speed to 0 if jumping if you want
    //and don't do anything if running or idle so it loops on its own
    }

    Note: This does effectively reduce 1 step from the animation. If you be sure to draw the sprite before calling this code, then you effectively saved the step and the player will see the full animation.
     
  3. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Use states - there are loads of tutorials for these if you go look for them.
    Basically you would change to the attacking state and switch the sprite to be your attack sprite. The attack state would only ever switch back to the idle/normal sprite and state when the attack animation ends (you can use the end animation event and just check what the sprite is in there).
    Why do you want to do everything in the Draw Event? That seems counter-intuitive as you should be trying to keep as much logic out of the Draw Event, and you don't need to use it as there are other events better suited to doing the job.
     
  4. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    619
    Okay but how do I actually CHANGE the sprite? I can't find the command, or what I'm doing just isn't working for some reason.
     
  5. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    873
    Code:
    sprite_index = <name of new sprite>
    Simple as that.
    From the manual:
    http://docs.yoyogames.com/index.htm...stances/instance properties/sprite_index.html
     
    Dr_Nomz likes this.
  6. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    619
    Yeah I figured that out with some testing, also helped that I changed it to "sprite" index from "image" index. >_<

    I just left that question in case there was a better/different way of doing it, but what I have seems fine. I'll leave it open in case I need more help though.
     
  7. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    117
    Here is a simple and easy way to use states, though you will obviously need to elaborate on the code more, but it should give you the idea.

    Code:
    //Create Event
    enum States {
        Idle,
        Walk,
        Attack,
        Die
    }
    
    state = States.Idle;
    
    //Step Event
    if(<keypress> && state != States.Attack){
        state = States.Attack;
    }
    ...
    
    //Draw || Step Event
    switch (state){
        case States.Idle:
            swap_sprite(<sprite_idle>);
            break;
        case States.Walk:
            swap_sprite(<sprite_walk>);
            break;
        case States.Attack:
            swap_sprite(<sprite_attack>);
            break;
        case States.Die:
            swap_sprite(<sprite_die>);
            break;
    }
    
    //Script sprite_swap(<sprite_index>)
    if(sprite_index != argument[0]){
        sprite_index = <spr_idle>;
        image_index = 0; //reset animation to beginning
    }
     

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