[SOLVED] Making an attack animation play once.

Discussion in 'Programming' started by Arakei, Sep 12, 2019.

  1. Arakei

    Arakei Member

    Joined:
    Jul 18, 2019
    Posts:
    4
    Hi all! Still fairly new to GML and following tutorials and such online. Currently I'm playing with a little platformer, and have some code for the animations of the player object. However I'd like to add an attack animation that plays through the animation once when the attack button is pressed, and then reverts back. My code minus the attack at the moment looks like this;

    Code:
    if (!onground)
    {
        if (onwall != 0)
        {
            sprite_index = spr_playerwall;
            image_xscale = onwall;  
        }
        else
        {
            sprite_index = spr_playerjump;
            image_speed = 1;
            if (sign(vsp) > 0) sprite_index = spr_playerfall; else sprite_index = spr_playerjump;
        }
    }
    else
    {
        image_speed = 1;
        if (hsp == 0)
            { 
                sprite_index = spr_player;
            }
        else
        {
        sprite_index = spr_playerrun;    
        }
    }
    
    if (hsp != 0) image_xscale = sign(hsp);
    
    However I can't seem to insert an attack animation without breaking the rest of the code. I tried wrapping the whole code in an "If //attack pressed, else //rest of the code" but that didn't work.
    I put a simple "If //attack pressed, sprite_index = //Attack animation" However it only worked if I was holding down the button.
    Any suggestions where I'm going wrong would be much appreciated! Thank you!
     
  2. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    111
    your on the right track, essentially what you want is a state machine, the simplest way is like your originally thought method, when you hit attack button, set a variable to 1, if its 1 ignore all your other code and do this instead, at animation end if that variable is 1 set it to 0.
     
  3. Arakei

    Arakei Member

    Joined:
    Jul 18, 2019
    Posts:
    4
    Aha! Thank you. I didn't think of a state machine and I've got it working now! Thank you!
     

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