Jump to content

event-based keyboard handling


Areg
 Share

Recommended Posts

I know from examples that there are ways to capture keyboard events in the update() function of the game( that is, polling of events). But is there a way in phaser to register listeners to all keys/specific keys of keyboard. For example I created on-screen keyboard for my game, but I also want to listen to the physical keyboard. Thanks

Link to comment
Share on other sites

The example I provided is an event-based approach, but it depends what you're trying to do. For discreet inputs like taps, clicks or single keypresses events are the way to go, but for constant inputs like movement, where you typically hold an input down as long as the action should be relevant, then polling works better than having potentially messy hold and release events.

Link to comment
Share on other sites

I think it's mainly a conceptual thing. It takes less code and is more reliable to have an 'isDown' boolean polled in a game loop for actions which require you to hold the button for an unspecified length of time. The same is also true for events to be fired when something 'happens' rather than when something is 'happening' - if you already have a game loop running, then it seems wasteful to have an event like 'onHold' which is fired 60 times per second, or worse, at an arbitrary frequency. In that situation it's much better to have a function called once when the input is activated.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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