Recording double presses of buttons/keys

So not sure i’m asking in the right place, but:

In the platformer i’m trying to make i want to be able to have the player sprite double jump and dash, essentially a double move.

I’d like this to occur when the player presses the appropriate key twice in a short enough time frame. But i’m not sure how to monitor this.

Currently for key presses i’ve got (or will have) booleans that become ‘true’ when a key is pressed and change to false when it is released.

For the double jump (stick with this for now), should i be recording when the key is pressed or released last, ie lastJumpPress = (some call to nano time or current update count) then compare the next jump press to this value and if it is within my desired reaction time upgrade to the double move?

some crappy pseudo code

private boolean jump = false;
private boolean doubleJump = false;
private boolean moveLeft = false;
private int currentJumpTime = 0;
private int oldJumpTime = 0;
//other bools for other movements

...//other code

addKeyListener(new KeyAdapter(){
   public void keyPressed(KeyEvent e){
      processKeyPress(e);}
   public void keyReleased(KeyEvent e){
      processKeyRelease(e);}
   });

...//other code

private void processkeyPress(KeyEvent e){
   int KeyCode = e.getKeyCode();

   If(KeyCode == KeyEvent.VK_JUMP){      //it wont be 'jump' it will be VK_UP or something
      currentJumpTime = getCurrentUpdate();
      If((currentJumpTime - oldJumpTime) < 10)  //for example, will likely be <1/10 second
         doubleJump = true;
      else 
         jump = true;
      oldJumpTime = currentJumpTime;
}

private processKeyRelease(KeyEvent e){
   //something similar that switchs both booleans back to false when key released


//some other code that adds upwards velocity based on whether jump or doubleJump is true
//this movement will then begin after the crouch part in the animation of sprite
//the length of the crouch part of animation is also how quickly the double press must be made

Am i on the right track or have a made a horrible omission?

Thanks.

Here’s a corner case to consider. If, say, your absolute time is measured from the beginning of a new game or level (that is, getCurrentUpdate() returns 0 initially), then if the player jumps right away, it may register as a double jump even though it’s a single jump because the measured time interval will be sufficiently small.

In practice this would probably never happen because a) most absolute time functions return larger values (e.g. relative to some reference time), and b) the player is unlikely to press the jump key in the first tenth of a second of game play. But, if you care about conceptual correctness (rather than just pragmatism), then technically that would be a flaw in the current approach. To handle this case, I’d just consider the first press of the jump key in a game or level always to be a ‘single jump’ press, regardless of timing. (One possible easy solution would be to initialize the old jump time to a negative value with sufficient magnitude, assuming the time function will never return negative values.)

Also, based on your description it sounds like you only want the player to be able to jump when grounded (that is, in contact with something beneath them), but maybe you already have that covered.

Otherwise, it seems like you have the right idea (I didn’t proof your code that carefully though, so there may be issues I’ve overlooked).

Thanks Jesse.

As i was writing that it seemed to me that it should work. Still good to know other people (at least one) think so too. :slight_smile:

Yes, currently, i’m planning for the jump/doubleJump to be off a (horizontal) surface. None of this wall jumping or air jumping for Bogan. He’s just not that agile. Though it’s something to keep in mind if i ever get to the completion point and want to add characters. Or for the sequel…

Thanks for pointing out that corner case too. Though i don’t think it’s ever likely to happen it’s good to know it’s a possibility when i work on something else where it could be more likely. I think this program should have run through several updates (at a rate of 40 UPS) before any actual gameplay is possible, which would preclude it. Perhaps i should initialise OldJumpTime as - 10 or something anyway?

Also, don’t bother checking the code, i just mashed it out to illustrate what i meant.

So now i know this approach should work i can move on to other questions. :slight_smile: