I want to code an object that, when the left mouse button it pressed on it, starts moving towards (x,y) point and when it reaches the (x,y) point it stops moving. I order to object when it's clicked by the left mouse button. The object moves towards (x,y) with 75 px/s speed but it doesn't stop at (x,y) point, it keeps moving on.
Asked
Active
Viewed 1,047 times
-1
-
you need to add your efforts and explain in detail what you want to do – VedantK Nov 21 '15 at 15:05
-
Please read http://stackoverflow.com/help/how-to-ask – zeeMonkeez Nov 21 '15 at 15:06
1 Answers
2
You can use something like this:
Create Event:
moving = false;
moving_speed = 4;
target_x = 0;
target_y = 0;
Global Mouse Left Pressed Event:
target_x = mouse_x;
target_y = mouse_y;
moving = true;
sprite_index = spr_walk; // Start animation
image_speed = 0.5; // Animation speed
Step Event:
if moving and point_distance(x, y, target_x, target_y) > moving_speed
{
dir = point_direction(x, y, target_x, target_y);
x += lengthdir_x(moving_speed, dir);
y += lengthdir_y(moving_speed, dir);
}
else
{
moving = false;
x = target_x;
y = target_y;
image_speed = 0; // Stop animation
sprite_index = spr_stay;
}

Dmi7ry
- 1,777
- 1
- 13
- 25