GML Animation

D

DarthTenebris

Guest
Hello everybody,

It's been a quite a while since I last spent time with Game Maker, and I've definitely gone rusty. I'm trying to get my player animated when he is walking and stop when he isn't, but so far it's not working, and I have no idea why.

Code:
// obj_player Create Event:
sprite_index = spr_player_down;
image_speed = 0;
image_index = 0;

spd = 1;

facing = 1; // 0 = right, 1 = down, 2 = left, 3 = up

keyLeft = ord("A");
keyRight = ord("D");
keyUp = ord("W");
keyDown = ord("S");

// obj_player Step Event:
switch(keyboard_check(keyRight) - keyboard_check(keyLeft)) {
    case 0: {
        image_speed = 0;
        image_index = 0;
    } break;
  
    case 1: {
        image_speed = 1;
        facing = 0;
        x += spd;
    } break;
  
    case -1: {
        image_speed = 1;
        facing = 2;
        x -= spd;
    } break;
}

switch(keyboard_check(keyDown) - keyboard_check(keyUp)) {
    case 0: {
        image_speed = 0;
        image_index = 0;
    } break;
  
    case 1: {
        image_speed = 1;
        facing = 1;
        y += spd;
    } break;
  
    case -1: {
        image_speed = 1;
        facing = 3;
        y -= spd;
    } break;
}

switch(facing) {
    case 0: {
        sprite_index = spr_player_right;
    } break;
      
    case 1: {
        sprite_index = spr_player_down;
    } break;
      
    case 2: {
        sprite_index = spr_player_left;
    } break;
      
    case 3: {
        sprite_index = spr_player_up;
    } break;
}
This results in the player changing sprites correctly to face the correct direction, however the animation doesn't play. Interestingly, if I go in diagonals, it does the animation, but not if I only go horizontally or vertically. What am I doing wrong?

Thank you for your time.

=========================
EDIT: Ok I'm dumb. I found where my mistake was: checking left/right separate from up/down the way I was doing it didnt work, anything the left/right check did was overridden by the up/down check. My not-so-quick fix was to manually dictate every single one of the possible combination of key presses (all 16 of them, 4 keys with 2 states each). This does fix my problem, but now the question is: is there a more elegant solution?
 
Last edited by a moderator:
D

DarthTenebris

Guest
Ok I'm dumb. I found where my mistake was: checking left/right separate from up/down the way I was doing it didnt work, anything the left/right check did was overridden by the up/down check. My not-so-quick fix was to manually dictate every single one of the possible combination of key presses (all 16 of them, 4 keys with 2 states each). This does fix my problem, but now the question is: is there a more elegant solution?
 
Top