Jump to content

How does Key._justDown work?


BlueLemming
 Share

Recommended Posts

I've been struggling to figure out if the "_justDown" property of a key is working properly or not.

It seems to always be true as long as the key is held just like "isDown" when accessed directly (ex: someKey._justDown). It seems that the intention is to use the Phaser.Input.Keyboard.JustDown() getter method, but it returns the same results as key.isDown and key._justDown (ex: Phaser.Input.Keyboard.JustDown(someKey))

If this is a bug then I'm comfortable enough to try and make a fix, but I figured I should first see if I'm misunderstanding how to use it in the first place.

(Using Phaser 3 beta 19)

Link to comment
Share on other sites

I was able to make my own quick solution for detecting when a key was first pressed. I used lodash for a helper function, apologies if you don't want to use lodash. You'll just have to rewrite the "getKeyByKeycode" function if so.

// snipped from my ES6 Scene class
import _find from 'lodash/find'

export default class extends Phaser.Scene {
  create() {
    this.keys = this.input.keyboard.addKeys({
      left: Phaser.Input.Keyboard.KeyCodes.LEFT,
      right: Phaser.Input.Keyboard.KeyCodes.RIGHT,
      jump: Phaser.Input.Keyboard.KeyCodes.A
    })

    this.input.keyboard.on('keydown', event => {
      const key = this.getKeyByKeyCode(event.keyCode)

      if (key) {
        if (key.timeUp === 0 || key.timeDown < key.timeUp) {
          if (key.timeUp === 0) {
            key.timeUp = 1
          }
          this.input.keyboard.emit('keypressed', event)
        }
      }
    })
  }

  getKeyByKeyCode(keyCode) {
    return _find(this.keys, { keyCode })
  }
}

 

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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