4 directional Idle animation

DannyDuque

Member
I was trying to make an animated sprite, and I was able to create the running animation and an idle animation that plays when the character stops moving. However, no matter what direction I input, only the right facing animation plays when I have frames for up, down, and left as well. Is there any way to get the correct facing animation to play when I release a certain key?
Video
(For reference, this object is 2 sprites. The Run sprite has 16 frames (4 for each direction) and the idle sprite is supposed to have 8 frames (2 for each direction, but in the video only the first two frames loop.))

GML:
//keyboard check
keyLeft = keyboard_check (vk_left);
keyRight = keyboard_check (vk_right);
keyUp = keyboard_check (vk_up);
keyDown = keyboard_check(vk_down);
keyActivate = keyboard_check_pressed (ord("X"));

inputDirection = point_direction(0,0,keyRight-keyLeft,keyDown-keyUp);
inputMagnitude = (keyDown - keyUp != 0) || (keyRight - keyLeft != 0);

//movement

hspeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vspeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

var _oldSprite = sprite_index;
if (inputMagnitude != 0)
{
    direction = inputDirection;
    sprite_index = runsprite;
}else sprite_index = idlesprite;
if (_oldSprite != sprite_index) localFrame = 0;

var _cardinalDirection = round(direction/90);
var _totalFrames = sprite_get_number(sprite_index) / 4;
image_index = localFrame + (_cardinalDirection * _totalFrames);
localFrame += sprite_get_speed(sprite_index) / 240;

//if animation loops on next game step

if (localFrame >= _totalFrames)
{
    animationEnd = true;
    localFrame -= _totalFrames;
}else animationEnd = false;
 
Top