Jump to content

workaround for keyboard.justPressed?


Stemkoski
 Share

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

        //somewhere during initialization:        var keyReset = false;        //in update:        if (game.input.keyboard.justPressed(Phaser.Keyboard.ONE) && !keyReset) {            keyReset = true;            console.log("is down 1");        }        if (game.input.keyboard.justReleased(Phaser.Keyboard.ONE)) {            keyReset = false;        }

The old fashioned way  ;)

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

 Share

  • Recently Browsing   0 members

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