Skip to content

Commit d1a8ecf

Browse files
committed
Added justDown access to _justDown internal boolean. Records whether the key has been pressed since the last time it was checked. Resets to false only when it is read. Used for anything that needs to know when a key is first pressed with no autorepeat, without using closures or callbacks.
1 parent 7bd0a8d commit d1a8ecf

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/input/Key.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ Phaser.Key = function (game, keycode) {
4444
*/
4545
this.isUp = true;
4646

47+
/**
48+
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
49+
* @private
50+
*/
51+
this._justDown = false;
52+
4753
/**
4854
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
4955
* @default
@@ -145,6 +151,7 @@ Phaser.Key.prototype = {
145151

146152
this.event = event;
147153

154+
// exit if this key down is from auto-repeat
148155
if (this.isDown)
149156
{
150157
return;
@@ -160,6 +167,10 @@ Phaser.Key.prototype = {
160167
this.duration = 0;
161168
this.repeats = 0;
162169

170+
// _justDown will remain true until it is read via the justDown Getter
171+
// this enables the game to poll for past presses, or reset it at the start of a new game state
172+
this._justDown = true;
173+
163174
this.onDown.dispatch(this);
164175

165176
},
@@ -249,6 +260,28 @@ Phaser.Key.prototype = {
249260

250261
};
251262

263+
264+
/**
265+
* Getter / Setter for _justDown property.
266+
* Reading justDown will reset it to false
267+
* @property {boolean} justDown
268+
* @memberof Phaser.Key
269+
* @default false
270+
*/
271+
Object.defineProperty(Phaser.Key.prototype, "justDown", {
272+
273+
get: function () {
274+
var r = this._justDown;
275+
this._justDown = false;
276+
return r;
277+
},
278+
set: function (value) {
279+
this._justDown = value;
280+
},
281+
282+
});
283+
284+
252285
/**
253286
* An enabled key processes its update and dispatches events.
254287
* A key can be disabled momentarily at runtime instead of deleting it.

0 commit comments

Comments
 (0)