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 #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 #1321)
This commit is contained in:
photonstorm 2014-11-13 13:01:58 +00:00
parent b855df2656
commit 527934d909
3 changed files with 89 additions and 31 deletions

View file

@ -112,6 +112,8 @@ Version 2.2.0 - "Bethal" - in development
* Wheel Scroll Event (old non-FF) and DOM Mouse Wheel (old FF) are
supported via a non-exported reused wrapper object; WheelEventProxy.
The proxy methods are generated one-time dynamically but only when needed.
* 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)
* 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)
### Updates
@ -146,6 +148,8 @@ The proxy methods are generated one-time dynamically but only when needed.
* AudioSprite - removed an unnecessary if-statement (thanks @DaanHaaz #1312)
* 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.
* Phaser.DOM now houses new DOM functions. Some have been moved over from ScaleManager as appropriate.
* 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.
### Bug Fixes

View file

@ -33,23 +33,17 @@ Phaser.Key = function (game, keycode) {
this.event = null;
/**
* @property {boolean} isDown - The "down" state of the key.
* @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.
* @default
*/
this.isDown = false;
/**
* @property {boolean} isUp - The "up" state of the key.
* @property {boolean} isUp - The "up" state of the key. This will remain `true` for as long as the keyboard thinks this key is up.
* @default
*/
this.isUp = true;
/**
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
* @private
*/
this._justDown = false;
/**
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
* @default
@ -118,10 +112,28 @@ Phaser.Key = function (game, keycode) {
*/
this.onUp = new Phaser.Signal();
/**
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
* @private
*/
this._justDown = false;
/**
* @property {boolean} _justUp - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
* @private
*/
this._justUp = false;
};
Phaser.Key.prototype = {
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#update
* @protected
*/
update: function () {
if (!this._enabled) { return; }
@ -141,8 +153,9 @@ Phaser.Key.prototype = {
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#processKeyDown
* @param {KeyboardEvent} event.
* @param {KeyboardEvent} event - The DOM event that triggered this.
* @protected
*/
processKeyDown: function (event) {
@ -177,8 +190,9 @@ Phaser.Key.prototype = {
/**
* Called automatically by Phaser.Keyboard.
*
* @method Phaser.Key#processKeyUp
* @param {KeyboardEvent} event.
* @param {KeyboardEvent} event - The DOM event that triggered this.
* @protected
*/
processKeyUp: function (event) {
@ -197,6 +211,10 @@ Phaser.Key.prototype = {
this.timeUp = this.game.time.time;
this.duration = this.game.time.time - this.timeDown;
// _justUp will remain true until it is read via the justUp Getter
// this enables the game to poll for past presses, or reset it at the start of a new game state
this._justUp = true;
this.onUp.dispatch(this);
},
@ -219,6 +237,8 @@ Phaser.Key.prototype = {
this.timeUp = this.game.time.time;
this.duration = 0;
this._enabled = true; // .enabled causes reset(false)
this._justDown = false;
this._justUp = false;
if (hard)
{
@ -231,12 +251,14 @@ Phaser.Key.prototype = {
},
/**
* Returns the "just pressed" state of the Key. Just pressed is considered true if the key was pressed down within the duration given.
* @method Phaser.Key#justPressed
* @param {number} [duration=50] - The duration below which the key is considered as being just pressed.
* @return {boolean} True if the key is just pressed otherwise false.
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` is it either isn't down,
* or was pressed down longer ago than then given duration.
*
* @method Phaser.Key#downDuration
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms.
* @return {boolean} True if the key was pressed down within the given duration.
*/
justPressed: function (duration) {
downDuration: function (duration) {
if (typeof duration === "undefined") { duration = 50; }
@ -245,12 +267,14 @@ Phaser.Key.prototype = {
},
/**
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given.
* @method Phaser.Key#justReleased
* @param {number} [duration=50] - The duration below which the key is considered as being just released.
* @return {boolean} True if the key is just released otherwise false.
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` is it either isn't down,
* or was pressed down longer ago than then given duration.
*
* @method Phaser.Key#upDuration
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms.
* @return {boolean} True if the key was released down within the given duration.
*/
justReleased: function (duration) {
upDuration: function (duration) {
if (typeof duration === "undefined") { duration = 50; }
@ -260,10 +284,12 @@ Phaser.Key.prototype = {
};
/**
* Getter / Setter for _justDown property.
* Reading justDown will reset it to false
* The justDown value allows you to test if this Key has just been pressed down or not.
* When you check this value it will return `true` if the Key is down, otherwise `false`.
* 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.
*
* @property {boolean} justDown
* @memberof Phaser.Key
* @default false
@ -271,20 +297,41 @@ Phaser.Key.prototype = {
Object.defineProperty(Phaser.Key.prototype, "justDown", {
get: function () {
var r = this._justDown;
var current = this._justDown;
this._justDown = false;
return r;
},
set: function (value) {
this._justDown = value;
},
return current;
}
});
/**
* The justUp value allows you to test if this Key has just been released or not.
* When you check this value it will return `true` if the Key is up, otherwise `false`.
* You can only call justUp once per key release. 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.
*
* @property {boolean} justUp
* @memberof Phaser.Key
* @default false
*/
Object.defineProperty(Phaser.Key.prototype, "justUp", {
get: function () {
var current = this._justUp;
this._justUp = false;
return current;
}
});
/**
* An enabled key processes its update and dispatches events.
* A key can be disabled momentarily at runtime instead of deleting it.
*
* @property {boolean} enabled
* @memberof Phaser.Key
* @default true
@ -292,9 +339,13 @@ Object.defineProperty(Phaser.Key.prototype, "justDown", {
Object.defineProperty(Phaser.Key.prototype, "enabled", {
get: function () {
return this._enabled;
},
set: function (value) {
value = !!value;
if (value !== this._enabled)
@ -303,6 +354,7 @@ Object.defineProperty(Phaser.Key.prototype, "enabled", {
{
this.reset(false);
}
this._enabled = value;
}
}

View file

@ -1689,6 +1689,8 @@ declare module Phaser {
game: Phaser.Game;
isDown: boolean;
isUp: boolean;
justDown: boolean;
justUp: boolean;
keyCode: number;
onDown: Phaser.Signal;
onHoldCallback: Function;
@ -1699,12 +1701,12 @@ declare module Phaser {
timeDown: number;
timeUp: number;
justPressed(duration?: number): boolean;
justReleased(duration?: number): boolean;
downDuration(duration?: number): boolean;
processKeyDown(event: KeyboardEvent): void;
processKeyUp(event: KeyboardEvent): void;
reset(hard?: boolean): void;
update(): void;
upDuration(duration?: number): boolean;
}