Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key.duration is not updated when key is down/up #4484

Closed
belen-albeza opened this issue Apr 22, 2019 · 1 comment
Closed

Key.duration is not updated when key is down/up #4484

belen-albeza opened this issue Apr 22, 2019 · 1 comment

Comments

@belen-albeza
Copy link

Version

  • Phaser Version:
    v3.16.2-FB
    Place the operating system below this comment.
  • Operating system:
    Mac OS Sierra
  • Browser: (I tried with Firefox and Chrome, it happens in both of them)

Description

Key.duration isn't updated when the key is being held down. As a consequence, Keyboard.DownDuration and Keyboard.UpDuration don't work as intended either.

Looking at the source, it seems duration is only updated for resetting it to zero when the key has just been pressed down (https://github.com/photonstorm/phaser/blob/v3.16.2/src/input/keyboard/keys/Key.js#L269), but not when we keep holding it, and later on when the key is up.

I'm not sure if this is the intended behavior for Key.duration, but DownDuration and UpDuration should work regardless.

Example Test Code

Online snippet with full code at: https://codepen.io/ladybenko/pen/KYBRrw?editors=0010

Relevant part from above:

function create() 
{
    // text labels
  this.labelStatus = this.add.text(10, 10, '');
  this.labelDuration = this.add.text(10, 50, '');
  this.labelDownDuration = this.add.text(10, 90, '')

  this.spaceKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
}

function update() {
  this.labelStatus.setText(`<SPACE> is ${this.spaceKey.isDown ? 'down' : 'up'}`);
  this.labelDuration.setText(`${this.spaceKey.duration} ms`);
  this.labelDownDuration.setText(`DownDuration(<SPACE>, 1000): ${Phaser.Input.Keyboard.DownDuration(this.spaceKey, 1000)}`);
}

Additional Information

I can try to send a PR to fix this, but I'd need to know if Key.duration should be updated each frame, or I should make DownDuration and UpDuration work around that (in this case, probably Key.timeDown / Key.timeUp, which contain a timestamp)

@photonstorm
Copy link
Collaborator

Thanks for this. Key.duration should only be changed when the key changes state (down to up), as Keys are not polled constantly while down. However, I've added a Key.getDuration method that will give you the value you need and updated the docs.

DownDuration was indeed broken and has been updated to use the game time, so you can call it at any stage now. UpDuration has been changed to do the same. These changes are all in the master branch now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants