GML [SOLVED] if keyboard_check_pressed then move_towards_point not stopping obj

Discussion in 'Programming' started by MartinK12, Jun 27, 2018.

  1. MartinK12

    MartinK12 Member

    Joined:
    Feb 26, 2017
    Posts:
    36
    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
     
  2. Bros Before Giraffes

    Bros Before Giraffes Member

    Joined:
    Jun 24, 2016
    Posts:
    30
    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;
    }
     
    MartinK12 likes this.
  3. Pyxus

    Pyxus Member

    Joined:
    Nov 6, 2016
    Posts:
    195
    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.
     
    MartinK12 likes this.
  4. Bros Before Giraffes

    Bros Before Giraffes Member

    Joined:
    Jun 24, 2016
    Posts:
    30
    Yeah that's true too. Setting the variable walksp to 0 will not affect the object's movement speed until you use it in another motion function
     
    MartinK12 and Pyxus like this.
  5. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    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 likes this.
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,909
    Bros and Pyxus are both right -- you need to follow both of their advice, since your code is wrong two levels.
     
    MartinK12 likes this.
  7. MartinK12

    MartinK12 Member

    Joined:
    Feb 26, 2017
    Posts:
    36
    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;
    }
    
     
  8. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,185
    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: Jun 27, 2018
    MartinK12 likes this.
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,909
    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);
    }
     
    MartinK12 likes this.
  10. MartinK12

    MartinK12 Member

    Joined:
    Feb 26, 2017
    Posts:
    36
    Thank You guys for solutions. Really appreciate it :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice