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:
Step event:
and finally, end step event:
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;
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;
Code:
velocity[@ X] = clamp (velocity[@ X], -spd, spd);
velocity[@ Y] = clamp (velocity[@ Y], -spd, spd);
x += velocity[@ X];
y += velocity[@ Y];