CrazySam Posted November 1, 2013 Share Posted November 1, 2013 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 More sharing options...
powerfear Posted November 1, 2013 Share Posted November 1, 2013 I just made some test with justPressed and it seems to be bugged for me it return true for 30 frames no matter the duration that it be 1 or 250. So the only thing I can suggest you is wait for a fix or do it with your own code. Link to comment Share on other sites More sharing options...
beeglebug Posted November 1, 2013 Share Posted November 1, 2013 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 More sharing options...
rich Posted November 1, 2013 Share Posted November 1, 2013 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 }} FranXtrada and threeninenineone 2 Link to comment Share on other sites More sharing options...
beeglebug Posted November 1, 2013 Share Posted November 1, 2013 Thanks Rich, that makes much more sense to me now. I'll stand down on the "fix" Link to comment Share on other sites More sharing options...
justGoscha Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
6regor Posted May 11, 2014 Share Posted May 11, 2014 How would you go about restricting function shootBullet() to only getting called when the player sprite is overlapping with some other sprite? Link to comment Share on other sites More sharing options...
valueerror Posted May 11, 2014 Share Posted May 11, 2014 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 More sharing options...
Recommended Posts