Click-to-move. Stuck in walls

T

TheCatWithHands

Guest
I'm trying to make point and click. For moving I use this:

Global Mouse Left Pressed


target_x = mouse_x;
target_y = mouse_y;


Step

if(target_x != 0 && target_y != 0) {
move_towards_point(target_x, target_y, spd);
if(point_distance(x, y, target_x, target_y) < spd + 2) {
speed = 0;
exit;
}

And it works correctly.
But I have problems with walls. I added this:

if (place_meeting(x + spd, y + spd, obj_walls)) {
speed = 0;
}

Object stops, but after he sticks and doesn't react any mouse's click. I think problem is the condition place_meeting(x + spd, y + spd, obj_walls) is always true after meeting the wall, but I have no idea how to fix that. I tried to use the variable and change its meaning to start moving again, but it doesn't work. Videos I had seen solved the problem when key-moving was used, I don't unerstand how to employ that in my case (but I tried too).
P.S. I don't have any animation.
 
C

CedSharp

Guest
Your code is moving your object BEFORE checking if there is a wall. So you'll always end up in the wall before detecting it. You can probably just add the wall-testing before the moving like this:
Code:
if(target_x != 0 && target_y != 0) {
  // Make sure there is no wall before moving
  if(!place_meeting(x+spd, y+spd, obj_walls)) {
    move_towards_point(target_x, target_y, spd);
    if(point_distance(x, y, target_x, target_y) < spd+2) {
      speed = 0;
    }
  }
  else {
    speed = 0;
  }
}
Hopefully this helps you out :p
 
Top