Key now needs a reference to the plugin that created it. Also added getDuration method.

This commit is contained in:
Richard Davey 2019-04-24 10:47:51 +01:00
parent d7917789eb
commit 6b5383a007

View file

@ -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;
}
});