Skip to content

Commit 01ccbd9

Browse files
committed
Key.enabled boolean allows you to toggle if a Key processes its update method or dispatches any events without deleting and re-creating it.
1 parent e764be4 commit 01ccbd9

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ New Features
9999
* StateManager.restart allows you to quickly restart the *current* state, optionally clearing the world and cache.
100100
* Tilemap.removeTile(x, y, layer) lets you remove the tile at the given coordinates and updates the collision data.
101101
* Tilemap.removeTileWorldXY lets you remove the tile at the given pixel value coordinates and updates the collision data.
102+
* Key.enabled boolean allows you to toggle if a Key processes its update method or dispatches any events without deleting and re-creating it.
102103

103104

104105
Bug Fixes

src/input/Key.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Phaser.Key = function (game, keycode) {
1818
*/
1919
this.game = game;
2020

21+
/**
22+
* @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.
23+
* @default
24+
*/
25+
this.enabled = true;
26+
2127
/**
2228
* @property {object} event - Stores the most recent DOM event.
2329
* @readonly
@@ -110,6 +116,8 @@ Phaser.Key.prototype = {
110116

111117
update: function () {
112118

119+
if (!this.enabled) { return; }
120+
113121
if (this.isDown)
114122
{
115123
this.duration = this.game.time.now - this.timeDown;
@@ -131,6 +139,8 @@ Phaser.Key.prototype = {
131139
*/
132140
processKeyDown: function (event) {
133141

142+
if (!this.enabled) { return; }
143+
134144
this.event = event;
135145

136146
if (this.isDown)
@@ -160,6 +170,8 @@ Phaser.Key.prototype = {
160170
*/
161171
processKeyUp: function (event) {
162172

173+
if (!this.enabled) { return; }
174+
163175
this.event = event;
164176

165177
if (this.isUp)
@@ -188,6 +200,7 @@ Phaser.Key.prototype = {
188200
this.isUp = true;
189201
this.timeUp = this.game.time.now;
190202
this.duration = this.game.time.now - this.timeDown;
203+
this.enabled = true;
191204

192205
this.onDown.removeAll();
193206
this.onUp.removeAll();

0 commit comments

Comments
 (0)