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

Setting enabled = false on a key while it is down does not reset the isDown state #1190

Closed
lazd opened this issue Sep 13, 2014 · 2 comments
Closed

Comments

@lazd
Copy link

lazd commented Sep 13, 2014

If a key is disabled while it is down, the isDown property remains true. I would expect isDown to be set to false when the key is disabled so game code doesn't assume the key is pressed.

Test case

Run the following code in the console of any Phaser 2.x game and press X on your keyboard.

var xKey = game.input.keyboard.addKey(Phaser.Keyboard.X);
setTimeout(function() {  // The timeout is here to give you time to press X
  xKey.enabled = false;
  console.assert(xKey.isDown === false, 'The key should not be down when disabled');
}, 3000);

In 3 seconds, you should get an AssertionError:

Assertion failed: The key should not be down when disabled 

Workaround

Call xKey.reset() when disabling the key.

var xKey = game.input.keyboard.addKey(Phaser.Keyboard.X);
setTimeout(function() {  // The timeout is here to give you time to press X
  xKey.enabled = false;
  xKey.reset();
  console.assert(xKey.isDown === false, 'The key should not be down when disabled');
}, 3000);

Details

  • Phaser 2.1.0
  • Mac OS X 10.9.2
  • Chrome 37.0.2062.94
@lazd lazd changed the title Setting enabled = false on a key while it is down does not reset the down state Setting enabled = false on a key while it is down does not reset the isDown state Sep 13, 2014
@photonstorm
Copy link
Collaborator

This is only possible if we swap enabled to be a getter/setter, which we could certainly do. Feels like hell of an edge case though, and easily avoided by calling Key.reset and then disabling it. Will flag it as a change request and see about adding.

@lazd
Copy link
Author

lazd commented Sep 15, 2014

Sounds like Key.reset() is a good enough workaround for now.

I ended up creating a moveDisabled variable and checking that in the game loop. There was less code (enabling/disabling two keys) and it was simple enough for my use case.

pnstickne added a commit to pnstickne/phaser that referenced this issue Oct 29, 2014
- Added `enabled` getter; this resets the key (soft) and then disables they key
- Added `_enabled` property and updated internal usage
- Updated document for `reset`.
photonstorm added a commit that referenced this issue Oct 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants