Jump to content

key down edge detection... opinions please


InsaneHero
 Share

Recommended Posts

I keep running into this and figure that Phaser is a friendly framework so it could (should?) support it easily... I want to know when a key is just pressed.  I'd rather not use closures or callbacks because that doesn't fit my general coding style (one loop to rule them all) and often involves extra variables that I'd like to avoid.  I'm also not keen on the "justPressed" method because getting the duration right is dodgy, especially if my game is going to use slow motion, or drop a frame every now and again.

 

Does anyone have a problem if I do a pull request on code to add:

 

Key.edgeDown

- false by default

- set true when the key is pressed

- stays true until it is read, then flips to false again

- won't go true after it is read even if the key is still down and the auto-repeat triggers, until the key is released first

 

This would let me code directly into my update:

 

if (myKey.edgeDown)

    doSomethingOnceNoMatterHowLongTheKeyIsHeld_thenDoItAgainNextPress();

 

(and perhaps I'd want to read myKey.edgeDown in a pause/resume function to clear it).

 

Opinions?  Is this just unnecessary extra code given that onDown callbacks are available, or is it something that would improve Phaser for you?

 

The commit is ready - just need to know if people want it or not :)

Link to comment
Share on other sites

I like the feature but not the name :) I think "justDown" makes more sense (and fits with justPressed and justReleased, although it may now confuse what those 2 methods do).

 

Phaser uses getters and setters all over the place. Look at the bottom of Key.js at the "enabled" Object.defineProperty. That's how to do it.

Link to comment
Share on other sites

justDown is a good name for it but like you say it might cause confusion with the other two.

 

Ideally we could rename justPressed to 'recentlyPressed' which is a better description of what it does (is it "justPressed" if you use a duration of 10 minutes?  :P) but messing with the existing API is not a step to take lightly!

 

I used 'edge' because that's how I remember it from writing code to interpret the hardware signals (an "edge" is when a signal transitions from one state to another) but that's pretty obscure and only going to make sense to ancient old farts like me.

 

Given that it locks the state until it's read a fully descriptive name would be horrible: pressedSinceLastTimeYouChecked

Doesn't exactly trip off the tongue.

 

Ok, I'll change it to justDown and do the pull commit, feel free to change it if you come up with a better name!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...