Stopping Player After Pressing A Combination of Directional Keys

Discussion in 'Programming' started by Carminecopeland, Jan 11, 2019.

Tags:
  1. Carminecopeland

    Carminecopeland Member

    Joined:
    Jan 11, 2019
    Posts:
    6
    So I want to make the player stop in place when the opposite directional keys are pressed. So when Left is being pressed and then Right is pressed the player will stop moving. Same with up and down. I have that set up but I also want the combination of up, down, and left/right to stop the player. If I press up and down first and then the side keys the player stops but if I press right/left first and then up and down the player moves diagonally.

    Code:
    if  global.controls_right && global.controls_down && global.controls_up{
        x += my_speed*0;
        y += my_speed*0;
    }
    else if global.controls_left && global.controls_down && global.controls_up{
        x += my_speed*0;
        y += my_speed*0;
    }
    else if global.controls_left && global.controls_right{
        x += my_speed*0;
        y += my_speed*0;
    }
    else if global.controls_down && global.controls_up{
        x += my_speed*0;
        y += my_speed*0;
    }
    This is the code I have set up to do this. Any help to fix this would be appreciated.
     
  2. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    610
    Ex code
    horz_input = key_right - key_left;
    vert_input = key_down - key_up;
    x += horz_input * spd;
    y += vert_input * spd;

    I think
    if (horz_input != 0 && vert_input != 0)
    {
    // Prevent moving
    }
     
  3. ThijsLander

    ThijsLander Member

    Joined:
    Dec 2, 2018
    Posts:
    1
    Hi Carminecopeland,

    I think this is what you're looking for.
    Code:
    x += (right_key - left_key) * speed;
    y += (down_key - up_key) * speed;
    
    
    if you press right, the returned value is 1 times the desired speed.
    if you press left, it returns -1 times the desired speed.
    if both are pressed, 0 times the desired speed, which of course equals to 0, is returned.
    The same happens with the y axis.
     
  4. Carminecopeland

    Carminecopeland Member

    Joined:
    Jan 11, 2019
    Posts:
    6
    See, the bottom 2 parts of my code work. My problem is with the top two. I want it to be that when the combination of right/left+up+down, up and down can be swapped, are pressed in that order the player stops. As of now if I hit left or right and then up+down the player moves diagonally which is what I am trying to prevent. I have tried both your code and the person above your's post but neither worked to prevent this sadly.
     
  5. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    509
    If you do something like this:

    Code:
    hsp = (right_key - left_key) * my_speed;
    vsp = (up_key - down_key) * my_speed;
    
    if (hsp == 0) y += vsp;
    if (vsp == 0) x += hsp;
    
    This should work I think?
     
  6. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    220
    Code:
    up = keyboard_check_pressed(vk_up);
    down = keyboard_check_pressed(vk_down);
    left = keyboard_check_pressed(vk_left);
    right = keyboard_check_pressed(vk_right);
    
    if (up) && !(down) && !(left) &&!(right){ //up key is pressed the others aren't
    y -= my_speed;
    }
    
    if (down) && !(up)  && !(left) && !(right){ //down key is pressed, the others aren't
    y += my_speed;
    }
    
    if (left) && !(right) && !(down) && !(up){ //left key is pressed, the others aren't
    x -= my_speed;
    }
    
    if (right) && !(left) && (!down) && !(up){ //right key is pressed, the others aren't
    x += my_speed;
    }
    
    These lines of code only run if ONE key is pressed. If you were pressing for example left and then up, that means there are two keys being pressed, so nothing would happen, so the player would stand still.

    With this, you won't even need to set my_speed to 0 because it's implied (if the player is not meeting the requirements of any of these lines of code).

    Why wouldn't you want the player to move diagonally if you don't mind me asking? Most games nowadays use diagonal movement unless the game runs on a four-direction grid.

    P.S. If you decide to keep the my_speed*0 part... why not just put 0? It's the same thing, but shorter :p
     
  7. Carminecopeland

    Carminecopeland Member

    Joined:
    Jan 11, 2019
    Posts:
    6
    very true about the my_speed * 0 bit. Just didn't really put any thought into it. And I want the player to move diagonally, what I want to stop is when the player has the right or left key held down and then they hold down whilst also holding up they stop. I think I'm being terrible at explaining it. It's like stopping the player when they press up and down together except they are holding down right or left first. If I hold up and down first the player stops and if I press left or right afterwards the player doesn't move. That what I want except you press left or right first.
     
  8. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    220
    Well, try the code I gave you and let me know if it works. If the player is pressing up and down at once it's adding and subtracting the y value, thus creating 0 automatically. Same thing with left and right keys.

    To move diagonally, add this code along with the previous code I gave you
    Code:
    if (up) && (left) && !(down) && !(right){ //up and left keys
    x -= my_speed;
    y -= my_speed;
    }
    
    if (up) && (right) && !(down) && !(left){ //up and right keys
    x += my_speed;
    y -= my_speed;
    }
    
    if (down) && (left) && !(up) && !(right){ //down and left keys
    x -= my_speed;
    y += my_speed;
    }
    
    if (down) && (right) && !(up) && !(left){ //down and right keys
    x += my_speed;
    y += my_speed;
    } 
    If you find that the player moves twice as fast when going diagonally and you don't want that, then simply divide my_speed by 2:

    Code:
    x += my_speed/2;
    y += my_speed/2;
     

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