-1

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.

A. Campbell
  • 404
  • 2
  • 12

1 Answers1

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