GMS 2 [Solved] Double Tapping Movement Keys For Dash

Discussion in 'Programming' started by Sahibjot Cheema, Apr 27, 2018.

  1. Sahibjot Cheema

    Sahibjot Cheema Member

    Joined:
    Apr 24, 2018
    Posts:
    44
    I've already created dashing, I just wanted to know if anyone knows how to detect if right arrow or left arrow as been pressed twice in a short amount of time. Thanks
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,014
    You can set a timer to zero on key release. Ensure it counts up per step, and every time a key is pressed check if that timer is under 30 or so, which would indicate a double tap.
     
    mamacato likes this.
  3. Sahibjot Cheema

    Sahibjot Cheema Member

    Joined:
    Apr 24, 2018
    Posts:
    44
    I created this but theirs a bug. You can move to one direction for a while then let go and it will register as dash but I only want it to dash when you press the key twice fast. Is their a way to detect when a button is pressed but it can only be detected the first time?
     
  4. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,014
    Yep, use keyboard_check_press() will run code the first step it's pressed only.
    keyboard_check_pressed() will run code for every step it's being pressed.

    EDIT:
    Whoops, sorry, wrote this fast on a phone. keyboard_check_press was a mistake, I was meant to write keyboard_check_pressed. The context sensitive help should of showed only a few options anyway when you started typing it. Glad you sorted it out.
     
    Last edited: Apr 28, 2018
  5. Sahibjot Cheema

    Sahibjot Cheema Member

    Joined:
    Apr 24, 2018
    Posts:
    44
    I don't think keyboard_check_press() exists. It doesn't register in GM and I can't find it online either. Btw I found out keyboard_check_pressed() does what you thought keyboard_check_press() does. I just found this out and everything works perfectly. Wish I found this out sooner. Thx though :D

    Edit: I think you were trying to say this, "With this function you can check to see if a key has been pressed or not. Unlike the keyboard_check function, this function will only run once for every time the key is pressed down, so for it to trigger again, the key must be first released and then pressed again." It's from the GM wiki.
     
    Last edited: Apr 28, 2018
  6. ender22782

    ender22782 Guest

    Create a "state" variable on your object, and set it to "no press". On the Key Pressed event, check to see if it is currently set it to "no press" - if so, set it to "pressed" and start your timer; if it is already set to "pressed", set it back to "no press", stop your timer, and run your double-tap code. If you do not press the key the second time and the timer expires, have it run your single-press code, reset the timer, and set the variable back to "no press".
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,335
    The timer part is right, but unless you want right-leeft-dash or left-right-dash, then you will need to store the last key pressed or make sure the player is going the same direction.
     
  8. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,014
    Whoops, sorry, wrote this fast on a phone. keyboard_check_press was a mistake, I meant to write keyboard_check_pressed. The context sensitive help should of showed only a few options anyway when you started typing it. Glad you sorted it out.
     
  9. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,496
    I should mention that there are gesture events to help you with all that.
    It will allow you to only have taps registered as taps, double taps as double taps (without registering the first tap), and drags as drags (without registering the initial tap).
     
  10. AiChi

    AiChi Member

    Joined:
    Monday
    Posts:
    2
    I am trying to use this for running: first the keyboard_check_pressed will set the timer and if I press the same key again and timer is on, I can run. The problem is that the time is still counting and as soon as it's off, it stops my running session.

    Can you please clarify how did you achieve that? I keep failing to figure it out.

    Thanks a lot.
     
  11. AiChi

    AiChi Member

    Joined:
    Monday
    Posts:
    2
    EDIT: of course that after I gave up the searching clues online and tried some other options, I figured it out:


    Code:
    
        // set Timer for running
        if (right_run_released) or (left_run_released) {
            alarm[0] = 5;
        }
    
        // check left or right run and enable it
        if alarm[0] >= 0 {
            if right_run_pressed or left_run_pressed {
            ableToRun = true;
            }
        }
     
        // when running, double speed
        if ableToRun == true {
            maxSpeed = 10;
            sprite_index = sPlayerRun;
            if left_run_released or right_run_released {
            ableToRun = false;
            maxSpeed = 5;  
            }
        }
      
    It's working great, but I am not entirely sure if any line of this code is needless.. ?

    I am sorry for double post, I couldn't edit my previous message
     

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