So I've been working on a platforming engine these past few days, and now I'm trying to solve this little problem: my attack animation (which should play when I press the Z key) is acting quite strangely. If I press Z while standing or jumping, it plays the first frame and then returns to the previous sprite. If I press Z while walking, it plays a random frame (from a total of 4) and then returns to the walking sprite. I've googled it multiple times but none of the answers I found worked.
Here's the animations code:
if (hspd > 0) {
image_xscale = 1;
} else if (hspd < 0) {
image_xscale = -1;
}
if (state == "walk") sprite_index = spr_walk;
if (state == "air") sprite_index = spr_jump;
if (state == "idle") sprite_index = spr_idle;
if (state == "attack") {
canAttack = false;
sprite_index = spr_attack1;
}
And here, the states code:
if (hspd == 0 && on_ground) {
state = "idle";
}
if (hspd != 0 && on_ground) {
state = "walk";
}
if (!on_ground) {
state = "air";
}
if (keyboard_check(ord('Z')) && canAttack) {
state = "attack";
}
This is probably a pretty simple question, but I'm stuck. I'd really appreciate any help!