• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] if keyboard_check_pressed then move_towards_point not stopping obj

MartinK12

Member
If player pressed "Z" then obj_player should move towards x,y of the obj_pos_z and if within 10 pixels from it, obj_player should stop.

In obj_player in step event there's only below code, but obj_player won't stop and if player presses "Z" again obj_player change direction and move towards x,y of obj_pos_z again an so on.

Code:
if keyboard_check_pressed(ord("Z"))
{
if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10
    {
    move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
    }
    else
    {
    walksp = 0;
    }
}
I know here's some basic bug but I'm tired of this code. I tried to modify it in so many ways and it's not working :( But it should work. There must be some logic problem but I can't see it.

Please help me bcs I don't understand where is the bug and why it don't work.
Thank You
 
the else is only checked for the step when z is pressed. Try putting something like this outside of the keyboard_check_pressed code block:

Code:
if point_distance(x, y, obj_pos_z.x, obj_pos_z.y) > 10
{
speed=0;
}
 
P

Pyxus

Guest
If player pressed "Z" then obj_player should move towards x,y of the obj_pos_z and if within 10 pixels from it, obj_player should stop.

In obj_player in step event there's only below code, but obj_player won't stop and if player presses "Z" again obj_player change direction and move towards x,y of obj_pos_z again an so on.

Code:
if keyboard_check_pressed(ord("Z"))
{
if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10
    {
    move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
    }
    else
    {
    walksp = 0;
    }
}
I know here's some basic bug but I'm tired of this code. I tried to modify it in so many ways and it's not working :( But it should work. There must be some logic problem but I can't see it.

Please help me bcs I don't understand where is the bug and why it don't work.
Thank You
I dont use the motion functions often, but I think I can take a guess at whats happening. In the else you're setting the variable walksp to 0 when you should be setting the instance variable speed to 0.
 

CMAllen

Member
move_towards_point() function sets the object's *speed*. This is a built-in, instance variable that all objects will use to move themselves around. So when you use the move_to_point() function, you need to reset the speed variable back to zero when no further movement should take place.
 

MartinK12

Member
Thank You for all Your replies - I did suggested versions among plenty of others and still no working solution :( This shouldn't be so complicated :(

Below in the spoiler there are 4 versions and I add description in their comments.

version 3 - without keyboard_check - works correctly - when the room starts obj_player starts moving towards x,y of obj_pos_z and stops when distance is less than 10 but how to add here keyboard_check?

version 4 - with while - I think this should be the proper solution but it freezes the game when player press Z key.

My logic:
-player pressed Z key once and some code should take place (and this code should take place only when player pressed Z key once because there will be other functions later)
-then obj_player should move towards x,y of obj_pos_z while point to its distance is > 10
-and when it's =< 10 should set instance speed to 0

Code:
// version 1 with speed and not walksp - not working
// obj_player not stopping
if keyboard_check_pressed(ord("Z"))
{
    if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10
    {
    move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
    }
    else
    {
    speed = 0;
    }
}

// version 2 with speed=0 outside keyboard_check - not working
// obj_player moves only when player presses Z key again and again
if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10
{
speed = 0;
}

if keyboard_check_pressed(ord("Z"))
{
move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
}

// version 3 - with speed and without keyboard_check
// works fine and obj_player stops correctly but I need keyboard_check
if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10
{
move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
}
else
{
speed = 0;
}

// version 4 with while - freezes the game after pressing Z key
// I think this should be working and is the proper solution but why it freezes?
if keyboard_check_pressed(ord("Z"))
{
    while (point_distance(x, y, obj_pos_z.x,obj_pos_z.y) > 10)
    {
    move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
    }
    speed = 0;
}
 

Simon Gust

Member
create the variable "flag" in the create event and set it to false there.
Then try this code
Code:
if (keyboard_check_pressed(ord("Z")))
{
    flag = true;
}

if (flag)
{
   var dis = point_distance(x, y, obj_pos_z.x, obj_pos_z.y);
   var spd = min(walksp,  dis - 10);
   move_towards_point(obj_pos_z.x, obj_pos_z.y, spd);
   if (dis <= 10) flag = false;
}
I hope that even if the last if-statement fails, that the object does not get closer than 10 pixels from the target.

EDIT: I just tested it and flag is not set back to false, so you may have to use
Code:
if (dis <= 10.0001) flag = false;
floating point precision error probably :/
 
Last edited:

TheouAegis

Member
Code:
if point_distance(x, y, obj_pos_z.x,obj_pos_z.y) < walksp
{
speed = 0;
x = obj_pos_z.x;
y = obj_pos_z.y;
}

if keyboard_check_pressed(ord("Z"))
{
move_towards_point(obj_pos_z.x,obj_pos_z.y,walksp);
}
 
Top