In your code, you set image_angle
and direction
, but then change only y
coordinate so of course you wont see any changes to x
coordinate.
You can use built-in direction
and speed
variables and game-maker will move any object automatically, or you can apply lengthdir_x
and lengthdir_y
functions on x
and y
coordinates.
direction
and speed
example:
//create event
playerSpeed = 3;
//step event
image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;
speed = 0; //reset speed at start of each step event
//now you wont have to check if key has been released to stop object
if(keyboard_check(ord('W')))
{
//distance between player and target
distance = point_distance(x, y, mouse_x, mouse_y)
if distance < playerSpeed
{
//with this, object will move only to mouse position, not further
speed = distance;
}
else
{
speed = playerSpeed;
}
}
else if(keyboard_check(ord('S')))
{
speed = -playerSpeed;
}
lengthdir_x
and lengthdir_y
example:
// create event
playerSpeed = 3;
// step event
image_angle = point_direction(x, y, mouse_x, mouse_y);
if keyboard_check( ord("W") )
{
x += lengthdir_x( playerSpeed, image_angle );
y += lengthdir_y( playerSpeed, image_angle );
}
else if keyboard_check( ord("S") )
{
x += lengthdir_x( playerSpeed, image_angle-180 );
y += lengthdir_y( playerSpeed, image_angle-180 );
}
Please note that all game-maker functions use angle in degrees, with angle 0(360) at right side of circle, increasing counter-clockwise.
Any functions work properly with values both under and above 0 to 360 range, while for example -90 = 270 (360-90)
and 400 = 40 (400-360)
.
Beware of how game-maker checks true
and false
. Any value < than 0 will result as false
on check.