Jump to content

workaround for keyboard.justPressed?


Recommended Posts

I placed the following code within the update loop:

if ( game.input.keyboard.justPressed(Phaser.Keyboard.ONE) )
console.log("just pressed 1");

if ( game.input.keyboard.isDown(Phaser.Keyboard.TWO) )
console.log("is down 2");

The justPressed code continues to fire while the key is held down; it behaves just like isDown in this example. I posted an issue at GitHub to this effect, but in the meantime, does anybody know of a method/workaround to use Phaser to register just a single event per keypress?


Link to comment
Share on other sites

Yes, but the old fashioned way could require lots of booleans floating around, in which case I'd just use some Javascript I wrote a while ago, at https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/js/KeyboardState.js which manages the whole thing. But I'd really rather keep the number of dependencies small -- to just phaser.js, preferably :)

Link to comment
Share on other sites

All of my keyboard changes are now pushed to the dev branch.


In this example I would still expect justPressed / justReleased to carry on firing for the duration of the check (i.e. 30 times if you don't change the default duration).


However you can easily do what you require by creating a new Phaser.Key object like this:

muteKey = game.input.keyboard.addKey(Phaser.Keyboard.M);muteKey.onDown.add(toggleMute, this);

Key objects have a lot of useful properties (isDown, isUp, duration, etc) but also 2 signals you can listen to: onDown and onUp, both of which only fire once.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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