• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] Making an attack animation play once.

A

Arakei

Guest
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!
 
S

spoonsinbunnies

Guest
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.
 
A

Arakei

Guest
Aha! Thank you. I didn't think of a state machine and I've got it working now! Thank you!
 
Top