Skip to content

Commit 100b69e

Browse files
committed
Fix for Key.enabled issue phaserjs#1190
- Added `enabled` getter; this resets the key (soft) and then disables they key - Added `_enabled` property and updated internal usage - Updated document for `reset`.
1 parent cee5e2a commit 100b69e

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

src/input/Key.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Phaser.Key = function (game, keycode) {
2020
this.game = game;
2121

2222
/**
23-
* @property {boolean} enabled - An enabled key processes its update and dispatches events. You can toggle this at run-time to disable a key without deleting it.
24-
* @default
23+
* The enabled state of the key - see `enabled`.
24+
* @property {boolean} _enabled
2525
*/
26-
this.enabled = true;
26+
this._enabled = true;
2727

2828
/**
2929
* @property {object} event - Stores the most recent DOM event.
@@ -117,7 +117,7 @@ Phaser.Key.prototype = {
117117

118118
update: function () {
119119

120-
if (!this.enabled) { return; }
120+
if (!this._enabled) { return; }
121121

122122
if (this.isDown)
123123
{
@@ -140,7 +140,7 @@ Phaser.Key.prototype = {
140140
*/
141141
processKeyDown: function (event) {
142142

143-
if (!this.enabled) { return; }
143+
if (!this._enabled) { return; }
144144

145145
this.event = event;
146146

@@ -171,7 +171,7 @@ Phaser.Key.prototype = {
171171
*/
172172
processKeyUp: function (event) {
173173

174-
if (!this.enabled) { return; }
174+
if (!this._enabled) { return; }
175175

176176
this.event = event;
177177

@@ -190,11 +190,13 @@ Phaser.Key.prototype = {
190190
},
191191

192192
/**
193-
* Resets the state of this Key. This sets isDown to false, isUp to true, resets the time to be the current time and clears any callbacks
194-
* associated with the onDown and onUp events and nulls the onHoldCallback if set.
193+
* Resets the state of this Key.
194+
*
195+
* This sets isDown to false, isUp to true, resets the time to be the current time, and _enables_ the key.
196+
* In addition, if it is a "hard reset", it clears clears any callbacks associated with the onDown and onUp events and removes the onHoldCallback.
195197
*
196198
* @method Phaser.Key#reset
197-
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks that are bound to this Key. A hard reset will.
199+
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks; a hard reset will.
198200
*/
199201
reset: function (hard) {
200202

@@ -204,7 +206,7 @@ Phaser.Key.prototype = {
204206
this.isUp = true;
205207
this.timeUp = this.game.time.now;
206208
this.duration = 0;
207-
this.enabled = true;
209+
this._enabled = true; // .enabled causes reset(false)
208210

209211
if (hard)
210212
{
@@ -246,4 +248,31 @@ Phaser.Key.prototype = {
246248

247249
};
248250

251+
/**
252+
* An enabled key processes its update and dispatches events.
253+
* A key can be disabled momentarily at runtime instead of deleting it.
254+
* @property {boolean} enabled
255+
* @memberof Phaser.Key
256+
* @default true
257+
*/
258+
Object.defineProperty(Phaser.Key.prototype, "enabled", {
259+
260+
get: function () {
261+
return this._enabled;
262+
},
263+
set: function (value) {
264+
value = !!value;
265+
266+
if (value !== this._enabled)
267+
{
268+
if (!value)
269+
{
270+
this.reset(false);
271+
}
272+
this._enabled = value;
273+
}
274+
}
275+
276+
});
277+
249278
Phaser.Key.prototype.constructor = Phaser.Key;

0 commit comments

Comments
 (0)