From b6dcfaa8c221b33ae83c3f2b795169264f3a8fd1 Mon Sep 17 00:00:00 2001 From: Robin Poirier Date: Wed, 14 Dec 2016 15:51:32 -0400 Subject: [PATCH] Adding justPressed and justReleased functions for keys. --- v2-community/src/input/Key.js | 41 ++++++++++++++++++++++++++++++ v2-community/src/input/Keyboard.js | 26 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/v2-community/src/input/Key.js b/v2-community/src/input/Key.js index 38d29e1e6..bae8065a0 100755 --- a/v2-community/src/input/Key.js +++ b/v2-community/src/input/Key.js @@ -81,6 +81,14 @@ Phaser.Key = function (game, keycode) { */ this.timeUp = -2500; + /** + * If the key is up this value holds the duration of that key release and is constantly updated. + * If the key is down it holds the duration of the previous up session. + * @property {number} duration - The number of milliseconds this key has been up for. + * @default + */ + this.durationUp = -2500; + /** * @property {number} repeats - If a key is held down this holds down the number of times the key has 'repeated'. * @default @@ -148,6 +156,10 @@ Phaser.Key.prototype = { this.onHoldCallback.call(this.onHoldContext, this); } } + else + { + this.durationUp = this.game.time.time - this.timeUp; + } }, @@ -178,6 +190,7 @@ Phaser.Key.prototype = { this.isUp = false; this.timeDown = this.game.time.time; this.duration = 0; + this.durationUp = this.game.time.time - this.timeUp; this.repeats = 0; // _justDown will remain true until it is read via the justDown Getter @@ -210,6 +223,7 @@ Phaser.Key.prototype = { this.isUp = true; this.timeUp = this.game.time.time; this.duration = this.game.time.time - this.timeDown; + this.durationUp = 0; // _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 @@ -236,6 +250,7 @@ Phaser.Key.prototype = { this.isUp = true; this.timeUp = this.game.time.time; this.duration = 0; + this.durationUp = -2500; this._enabled = true; // .enabled causes reset(false) this._justDown = false; this._justUp = false; @@ -280,6 +295,32 @@ Phaser.Key.prototype = { return (!this.isDown && ((this.game.time.time - this.timeUp) < duration)); + }, + + /** + * Returns `true` if the Key was just pressed down this update tick, or `false` if it either isn't down, + * or was pressed down on a previous update tick. + * + * @method Phaser.Key#justPressed + * @return {boolean} True if the key was just pressed down this update tick. + */ + justPressed: function () { + + return (this.isDown && this.duration === 0); + + }, + + /** + * Returns `true` if the Key was just released this update tick, or `false` if it either isn't up, + * or was released on a previous update tick. + * + * @method Phaser.Key#justReleased + * @return {boolean} True if the key was just released this update tick. + */ + justReleased: function () { + + return (!this.isDown && this.durationUp === 0); + } }; diff --git a/v2-community/src/input/Keyboard.js b/v2-community/src/input/Keyboard.js index 5624dcd86..b20e6906a 100755 --- a/v2-community/src/input/Keyboard.js +++ b/v2-community/src/input/Keyboard.js @@ -528,6 +528,32 @@ Phaser.Keyboard.prototype = { }, + justPressed: function (keycode) { + + if (this._keys[keycode]) + { + return this._keys[keycode].justPressed(); + } + else + { + return null; + } + + }, + + justReleased: function (keycode) { + + if (this._keys[keycode]) + { + return this._keys[keycode].justReleased(); + } + else + { + return null; + } + + }, + /** * Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser. *