Skip to content

Commit 527934d

Browse files
committed
Key.justPressed has bee renamed to Key.downDuration which is a much clearer name for what the method actually does. See Key.justDown for a nice clean alternative.
Key.justReleased has bee renamed to Key.upDuration which is a much clearer name for what the method actually does. See Key.justUp for a nice clean alternative. Key.justDown allows you to test if a Key has just been pressed down or not. You can only call justDown once per key press. It will only return `true` once, until the Key is released and pressed down again. This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop (thanks @pjbaron phaserjs#1321) Key.justUp allows you to test if a Key has just been released or not. You can only call justUp once per key press. It will only return `true` once, until the Key is pressed down and released again. This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop (thanks @pjbaron phaserjs#1321)
1 parent b855df2 commit 527934d

3 files changed

Lines changed: 89 additions & 31 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Version 2.2.0 - "Bethal" - in development
112112
* Wheel Scroll Event (old non-FF) and DOM Mouse Wheel (old FF) are
113113
supported via a non-exported reused wrapper object; WheelEventProxy.
114114
The proxy methods are generated one-time dynamically but only when needed.
115+
* Key.justDown allows you to test if a Key has just been pressed down or not. You can only call justDown once per key press. It will only return `true` once, until the Key is released and pressed down again. This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop (thanks @pjbaron #1321)
116+
* Key.justUp allows you to test if a Key has just been released or not. You can only call justUp once per key press. It will only return `true` once, until the Key is pressed down and released again. This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop (thanks @pjbaron #1321)
115117

116118
### Updates
117119

@@ -146,6 +148,8 @@ The proxy methods are generated one-time dynamically but only when needed.
146148
* AudioSprite - removed an unnecessary if-statement (thanks @DaanHaaz #1312)
147149
* ArcadePhysics.skipQuadTree is now set to `true` by default. A QuadTree is a wonderful thing if the objects in your game are well spaced out. But in tightly packed games, especially those with tilemaps or single-screen games, they are a considerable performance drain and eat up CPU. We've taken the decision to disable the Arcade Physics QuadTree by default. It's all still in there and can be re-enabled via `game.physics.arcade.skipQuadTree = false`, but please only do so if you're sure your game benefits from this.
148150
* Phaser.DOM now houses new DOM functions. Some have been moved over from ScaleManager as appropriate.
151+
* Key.justPressed has bee renamed to Key.downDuration which is a much clearer name for what the method actually does. See Key.justDown for a nice clean alternative.
152+
* Key.justReleased has bee renamed to Key.upDuration which is a much clearer name for what the method actually does. See Key.justUp for a nice clean alternative.
149153

150154
### Bug Fixes
151155

src/input/Key.js

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,17 @@ Phaser.Key = function (game, keycode) {
3333
this.event = null;
3434

3535
/**
36-
* @property {boolean} isDown - The "down" state of the key.
36+
* @property {boolean} isDown - The "down" state of the key. This will remain `true` for as long as the keyboard thinks this key is held down.
3737
* @default
3838
*/
3939
this.isDown = false;
4040

4141
/**
42-
* @property {boolean} isUp - The "up" state of the key.
42+
* @property {boolean} isUp - The "up" state of the key. This will remain `true` for as long as the keyboard thinks this key is up.
4343
* @default
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-
5347
/**
5448
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
5549
* @default
@@ -118,10 +112,28 @@ Phaser.Key = function (game, keycode) {
118112
*/
119113
this.onUp = new Phaser.Signal();
120114

115+
/**
116+
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
117+
* @private
118+
*/
119+
this._justDown = false;
120+
121+
/**
122+
* @property {boolean} _justUp - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
123+
* @private
124+
*/
125+
this._justUp = false;
126+
121127
};
122128

123129
Phaser.Key.prototype = {
124130

131+
/**
132+
* Called automatically by Phaser.Keyboard.
133+
*
134+
* @method Phaser.Key#update
135+
* @protected
136+
*/
125137
update: function () {
126138

127139
if (!this._enabled) { return; }
@@ -141,8 +153,9 @@ Phaser.Key.prototype = {
141153

142154
/**
143155
* Called automatically by Phaser.Keyboard.
156+
*
144157
* @method Phaser.Key#processKeyDown
145-
* @param {KeyboardEvent} event.
158+
* @param {KeyboardEvent} event - The DOM event that triggered this.
146159
* @protected
147160
*/
148161
processKeyDown: function (event) {
@@ -177,8 +190,9 @@ Phaser.Key.prototype = {
177190

178191
/**
179192
* Called automatically by Phaser.Keyboard.
193+
*
180194
* @method Phaser.Key#processKeyUp
181-
* @param {KeyboardEvent} event.
195+
* @param {KeyboardEvent} event - The DOM event that triggered this.
182196
* @protected
183197
*/
184198
processKeyUp: function (event) {
@@ -197,6 +211,10 @@ Phaser.Key.prototype = {
197211
this.timeUp = this.game.time.time;
198212
this.duration = this.game.time.time - this.timeDown;
199213

214+
// _justUp will remain true until it is read via the justUp Getter
215+
// this enables the game to poll for past presses, or reset it at the start of a new game state
216+
this._justUp = true;
217+
200218
this.onUp.dispatch(this);
201219

202220
},
@@ -219,6 +237,8 @@ Phaser.Key.prototype = {
219237
this.timeUp = this.game.time.time;
220238
this.duration = 0;
221239
this._enabled = true; // .enabled causes reset(false)
240+
this._justDown = false;
241+
this._justUp = false;
222242

223243
if (hard)
224244
{
@@ -231,12 +251,14 @@ Phaser.Key.prototype = {
231251
},
232252

233253
/**
234-
* Returns the "just pressed" state of the Key. Just pressed is considered true if the key was pressed down within the duration given.
235-
* @method Phaser.Key#justPressed
236-
* @param {number} [duration=50] - The duration below which the key is considered as being just pressed.
237-
* @return {boolean} True if the key is just pressed otherwise false.
254+
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` is it either isn't down,
255+
* or was pressed down longer ago than then given duration.
256+
*
257+
* @method Phaser.Key#downDuration
258+
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms.
259+
* @return {boolean} True if the key was pressed down within the given duration.
238260
*/
239-
justPressed: function (duration) {
261+
downDuration: function (duration) {
240262

241263
if (typeof duration === "undefined") { duration = 50; }
242264

@@ -245,12 +267,14 @@ Phaser.Key.prototype = {
245267
},
246268

247269
/**
248-
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given.
249-
* @method Phaser.Key#justReleased
250-
* @param {number} [duration=50] - The duration below which the key is considered as being just released.
251-
* @return {boolean} True if the key is just released otherwise false.
270+
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` is it either isn't down,
271+
* or was pressed down longer ago than then given duration.
272+
*
273+
* @method Phaser.Key#upDuration
274+
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms.
275+
* @return {boolean} True if the key was released down within the given duration.
252276
*/
253-
justReleased: function (duration) {
277+
upDuration: function (duration) {
254278

255279
if (typeof duration === "undefined") { duration = 50; }
256280

@@ -260,41 +284,68 @@ Phaser.Key.prototype = {
260284

261285
};
262286

263-
264287
/**
265-
* Getter / Setter for _justDown property.
266-
* Reading justDown will reset it to false
288+
* The justDown value allows you to test if this Key has just been pressed down or not.
289+
* When you check this value it will return `true` if the Key is down, otherwise `false`.
290+
* You can only call justDown once per key press. It will only return `true` once, until the Key is released and pressed down again.
291+
* This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop.
292+
*
267293
* @property {boolean} justDown
268294
* @memberof Phaser.Key
269295
* @default false
270296
*/
271297
Object.defineProperty(Phaser.Key.prototype, "justDown", {
272298

273299
get: function () {
274-
var r = this._justDown;
300+
301+
var current = this._justDown;
275302
this._justDown = false;
276-
return r;
277-
},
278-
set: function (value) {
279-
this._justDown = value;
280-
},
303+
return current;
304+
305+
}
281306

282307
});
283308

309+
/**
310+
* The justUp value allows you to test if this Key has just been released or not.
311+
* When you check this value it will return `true` if the Key is up, otherwise `false`.
312+
* You can only call justUp once per key release. It will only return `true` once, until the Key is pressed down and released again.
313+
* This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop.
314+
*
315+
* @property {boolean} justUp
316+
* @memberof Phaser.Key
317+
* @default false
318+
*/
319+
Object.defineProperty(Phaser.Key.prototype, "justUp", {
320+
321+
get: function () {
322+
323+
var current = this._justUp;
324+
this._justUp = false;
325+
return current;
326+
327+
}
328+
329+
});
284330

285331
/**
286332
* An enabled key processes its update and dispatches events.
287333
* A key can be disabled momentarily at runtime instead of deleting it.
334+
*
288335
* @property {boolean} enabled
289336
* @memberof Phaser.Key
290337
* @default true
291338
*/
292339
Object.defineProperty(Phaser.Key.prototype, "enabled", {
293340

294341
get: function () {
342+
295343
return this._enabled;
344+
296345
},
346+
297347
set: function (value) {
348+
298349
value = !!value;
299350

300351
if (value !== this._enabled)
@@ -303,6 +354,7 @@ Object.defineProperty(Phaser.Key.prototype, "enabled", {
303354
{
304355
this.reset(false);
305356
}
357+
306358
this._enabled = value;
307359
}
308360
}

typescript/phaser.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,8 @@ declare module Phaser {
16891689
game: Phaser.Game;
16901690
isDown: boolean;
16911691
isUp: boolean;
1692+
justDown: boolean;
1693+
justUp: boolean;
16921694
keyCode: number;
16931695
onDown: Phaser.Signal;
16941696
onHoldCallback: Function;
@@ -1699,12 +1701,12 @@ declare module Phaser {
16991701
timeDown: number;
17001702
timeUp: number;
17011703

1702-
justPressed(duration?: number): boolean;
1703-
justReleased(duration?: number): boolean;
1704+
downDuration(duration?: number): boolean;
17041705
processKeyDown(event: KeyboardEvent): void;
17051706
processKeyUp(event: KeyboardEvent): void;
17061707
reset(hard?: boolean): void;
17071708
update(): void;
1709+
upDuration(duration?: number): boolean;
17081710

17091711
}
17101712

0 commit comments

Comments
 (0)