diff --git a/src/input/keyboard/keys/Key.js b/src/input/keyboard/keys/Key.js index 41d8df3ad..cd3f7806e 100644 --- a/src/input/keyboard/keys/Key.js +++ b/src/input/keyboard/keys/Key.js @@ -19,6 +19,7 @@ var Events = require('../events'); * @constructor * @since 3.0.0 * + * @param {Phaser.Input.Keyboard.KeyboardPlugin} plugin - The Keyboard Plugin instance that owns this Key object. * @param {integer} keyCode - The keycode of this key. */ var Key = new Class({ @@ -27,10 +28,19 @@ var Key = new Class({ initialize: - function Key (keyCode) + function Key (plugin, keyCode) { EventEmitter.call(this); + /** + * The Keyboard Plugin instance that owns this Key object. + * + * @name Phaser.Input.Keyboard.Key#plugin + * @type {Phaser.Input.Keyboard.KeyboardPlugin} + * @since 3.17.0 + */ + this.plugin = plugin; + /** * The keycode of this key. * @@ -142,6 +152,8 @@ var Key = new Class({ /** * The number of milliseconds this key was held down for in the previous down - up sequence. + * This value isn't updated every game step, only when the Key changes state. + * To get the current duration use the `getDuration` method. * * @name Phaser.Input.Keyboard.Key#duration * @type {number} @@ -339,6 +351,31 @@ var Key = new Class({ return this; }, + /** + * Returns the duration, in ms, that the Key has been held down for. + * + * If the key is not currently down it will return zero. + * + * The get the duration the Key was held down for in the previous up-down cycle, + * use the `Key.duration` property value instead. + * + * @method Phaser.Input.Keyboard.Key#getDuration + * @since 3.17.0 + * + * @return {integer} The duration, in ms, that the Key has been held down for if currently down. + */ + getDuration: function () + { + if (this.isDown) + { + return (this.plugin.game.loop.time - this.timeDown); + } + else + { + return 0; + } + }, + /** * Removes any bound event handlers and removes local references. * @@ -350,6 +387,8 @@ var Key = new Class({ this.removeAllListeners(); this.originalEvent = null; + + this.plugin = null; } });