GameMaker 8-directional movement help

J

Jtilley54321

Guest
When moving in my code, everything works (as in all 8 directions, including not going faster when moving diagonal) up until the point that I hold the left and right movement keys. For whatever reason, when I hold left and right, I just go right BUT when I hold up and down, I stop like i'm supposed to. Any idea on why this is?

Create event:
Code:
// Macro

#macro X 0
#macro Y 1

// Inputs
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));
up = keyboard_check(ord("W"));
down = keyboard_check(ord("S"));

// Movement
spd = 3;
fric = .1;
acc = 1;

velocity = [0, 0];

facing = 0;
Step event:
Code:
// Inputs
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));
up = keyboard_check(ord("W"));
down = keyboard_check(ord("S"));

// Movement

var dirX, dirY, dir;

// Reset the direction
dirX = 0;
dirY = 0;

if (right || left)
{
    dirX = right - left;
}

if (up || down)
{
    dirY = down - up;
}

// Calculate the direction that the player needs to move
dir = point_direction(0, 0, dirX, dirY);

// Move the player

var currentSpeed = point_distance(0, 0, velocity[@ X], velocity[@ Y]);

// Horizontal movement
if (right || left)
{
    if (abs(currentSpeed) >= spd)
    {
        velocity[@ X] = lengthdir_x(spd, dir);
    }
    else
    {
        velocity[@ X] += lengthdir_x(acc, dir);
    }
}
else
{
    velocity[@ X] = lerp(velocity[@ X], 0, fric)
}

// Vertical movement
if (up || down)
{
    if (abs(currentSpeed) >= spd)
    {
        velocity[@ Y] = lengthdir_y(spd, dir);
    }
    else
    {
        velocity[@ Y] += lengthdir_y(acc, dir);
    }
}
else
{
    velocity[@ Y] = lerp(velocity[@ Y], 0, fric);
}

// Rotate sprite
facing = point_direction(x, y, mouse_x, mouse_y);

image_angle = facing;
and finally, end step event:
Code:
velocity[@ X] = clamp (velocity[@ X], -spd, spd);
velocity[@ Y] = clamp (velocity[@ Y], -spd, spd);

x += velocity[@ X];
y += velocity[@ Y];
 
D

dannyjenn

Guest
I don't see anything in your code that would be causing the problem, so I don't know.

This is only a guess, but maybe your keyboard doesn't support the 'A' and 'D' keys simultaneously? Try running this:
Code:
if(keyboard_check(ord("A")) && keyboard_check(ord("D"))){
    show_debug_message("!");
}
Then hold both 'A' and 'D'... if nothing shows up in the console, then there's nothing you can do :( (edit - Well, you could fix the problem by changing the controls. It's generally best to allow for customizable controls anyway.)
 
Last edited by a moderator:
Top