• 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!

GML Sprite animation not working, new to GML and help would be appreciated.[SOLVED]

A

Alexander Olofsson

Guest
So yeah, title basically says it all, I've set a key to trigger an attack animation, but it won't work, I have an idle animation, running anim, jumping etc. I'm thinking the idle and running animations are screwing up, and if they do, any way to make a line of code to stop the anim whilst attacking? Here's the code;
Code:
//Platform Physics

var rkey = keyboard_check(ord('D'));
var lkey = keyboard_check(ord('A'));
var jkey = keyboard_check(vk_space);
var akey = keyboard_check(ord('F'));


//Check for ground, it's useful.
if (place_meeting(x, y+1, obj_solid)) {
    vspd = 0;
  
    //Jumping
    if (jkey) {
        vspd = - jspd;
    }
} else {
    //You need gravity to use your legs and jump, run n stuff.
    if (vspd < 10) {
        vspd += grav;
    }
}

//Want to use your legs?
    if (rkey) {
    hspd = spd
}
  
    if (lkey) {
    hspd = -spd;
}

//Wanna know if you're moving or not? Well here you go. (check if moving)
if ((!rkey && !lkey) || (rkey && lkey)) {
    hspd = 0;
}

//Attack Animation (not working?( I've watched dozens of videos, still not working));
if (akey) {
    sprite_index = spr_attack;
    image_speed = 1
}


//Don't know how to run without moving a single limb in your body? Well, here's a fix for that. (Sprite anims)

if (rkey || lkey) {
    sprite_index = spr_running;
    image_speed = 0.2;
} else {
    sprite_index = spr_stand;
    image_speed = 0.08
}

if (!place_meeting( x, y+1, obj_solid)) {
    sprite_index = spr_jumping;
    image_speed = 0;
}


//Don't want to be running backwards? Well here's the fix to that. (Look in other dircetions)

if (rkey) {
    image_xscale = 1;
} else if (lkey) {
    image_xscale = -1;
}
Step Event above. Tried key press & key release, animation end, will still not work.

Oh, if I forgot to tell you, I started with coding yesterday. I can a bit of coding, but I'm still improving and trying to learn!
 
S

ScrabbitRabbit

Guest
I think it's probably because you're using keyboard_check() which checks if a key is being held down. See if it works if you hold the attack button.

It might work if you change it to keyboard_check_pressed() but then it will will only play maybe a frame before resetting to the other sprite. What I'd do is go to the Create event and put atk = false. Then check if the attack key is pressed using keyboard_check_pressed() and set atk to true. Now you can either set the animation on the key press or if atk == true depending on how you want attacking to work as your game goes on but either way you need to set it back to false once the animation is over.
 
A

Alexander Olofsson

Guest
Hmm, it seems like other sprite animations cancel out the attack one. Because if I use keyboard_check_pressed() then only 1 frame plays then one from the other animation. I don't quite understand how I would do on the other section, I cannot get it right. I'll post my Create event too:
Code:
///variables
grav = 0.5
spd = 2
jspd = 6.75
hspd = 0
vspd = 0
hp = 100
dir = 0
energy = 100
knockback = false
atk = false
 

Simon Gust

Member
Hmm, it seems like other sprite animations cancel out the attack one. Because if I use keyboard_check_pressed() then only 1 frame plays then one from the other animation. I don't quite understand how I would do on the other section, I cannot get it right.
Sounds like you want to know about state machines. They are a godsent
 
A

Alexander Olofsson

Guest
AHA! I think this is it! When I stand still, I am basically forcing myself to well, stand still. I don't know how I would fix that though.. Like change to spr_stand and then not like use it all the time, but after I run or something along those lines...

Code:
//Don't know how to run without moving a single limb in your body? Well, here's a fix for that. (Sprite anims)

if (rkey || lkey) {
    sprite_index = spr_running;
    image_speed = 0.2;
} else {
    sprite_index = spr_stand;
    image_speed = 0.08
}

if (!place_meeting( x, y+1, obj_solid)) {
    sprite_index = spr_jumping;
    image_speed = 0;
}
 
A

Alexander Olofsson

Guest
Wow, thank you so much bro, that really helped me out, I'll test that out right away!
 
A

Alexander Olofsson

Guest
Why does this happen? It's driving me nuts. Why does it want a bracket there? (Or whatever it's called :S)
 
Top