Jump to content

JustPressed - only once


CrazySam
 Share

Recommended Posts

game.input.keyboard.justPressed takes in two parameters, a keyCode and a duration. Checking for a duration (even a really small one), causes my justPressed checks to trigger during multiple frames, when really I just want it to trigger once when the user presses the key down, and only trigger again when they release it and press it down again.

 

How can I achieve this effect with Phaser?

Link to comment
Share on other sites

I'm just taking a look at the source for this to see if I can issue a fix, but first can someone explain to me what the use case of the method is? How are you expecting it to behave?

 

I assume it is for events that should only fire once? If so, i'm not sure what the point of the duration parameter is at all. Would be better to just internally flag the event as fired, so it doesn't get fired again until the key is released/pressed again?

Link to comment
Share on other sites

I'm tempted to remove justPressed because so many people seem to use it in a way it's not meant to work :) It's literally just meant to report true/false if the key was recently pressed or not, that's it - it's not a state flag, it doesn't remember if you pressed it or called it previously, it just says "yes, the Key was down within duration". If you put that check into a loop then it'll report "yes, I'm still just down" for as long as duration is.

 

What you're trying to do needs to be done like this:

fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);fireButton.onDown.add(shootBullet, this);...function shootBullet() { // Will only be called once per key press. // Will be passed the full Key object. See Phaser.Key for properties.}

There are no global "onDown / onUp" events to listen to on the Keyboard itself because I didn't want to be firing them every single time a key is pressed, for every key, without good reason. I could consider adding them and a function to enable them perhaps, then you could do:

game.input.keyboard.onDown.add(processKey, this);...function processKey(key) {  if (key.keyCode == Phaser.SPACEBAR) {    // do something space bar related  }}
Link to comment
Share on other sites

  • 5 months later...

For me this still calls the shootBullet function multiple times... 


fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);fireButton.onDown.add(shootBullet, this);...function shootBullet() {// Will only be called once per key press.// Will be passed the full Key object. See Phaser.Key for properties.}

EDIT:

Fixed... there was a stupid error in my code. I added the listener on every update loop.

Link to comment
Share on other sites

  • 3 weeks later...

i'm sure there is a better way but this should also work :

 

just check for the overlap separately and if it overlaps set a custom variable "overlaps" to true - then you can check for this variable in the shootBullet() function ....

 

 

@justGotcha:   haha.. i would have done the same thing.. (adding it to the update function)  thx for the info ..would you mark you answer #6 as best answer?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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