Skip to content

Commit

Permalink
Key.enabled boolean allows you to toggle if a Key processes its updat…
Browse files Browse the repository at this point in the history
…e method or dispatches any events without deleting and re-creating it.
  • Loading branch information
photonstorm committed Apr 9, 2014
1 parent e764be4 commit 01ccbd9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ New Features
* StateManager.restart allows you to quickly restart the *current* state, optionally clearing the world and cache.
* Tilemap.removeTile(x, y, layer) lets you remove the tile at the given coordinates and updates the collision data.
* Tilemap.removeTileWorldXY lets you remove the tile at the given pixel value coordinates and updates the collision data.
* Key.enabled boolean allows you to toggle if a Key processes its update method or dispatches any events without deleting and re-creating it.


Bug Fixes
Expand Down
13 changes: 13 additions & 0 deletions src/input/Key.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Phaser.Key = function (game, keycode) {
*/
this.game = game;

/**
* @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.
* @default
*/
this.enabled = true;

/**
* @property {object} event - Stores the most recent DOM event.
* @readonly
Expand Down Expand Up @@ -110,6 +116,8 @@ Phaser.Key.prototype = {

update: function () {

if (!this.enabled) { return; }

if (this.isDown)
{
this.duration = this.game.time.now - this.timeDown;
Expand All @@ -131,6 +139,8 @@ Phaser.Key.prototype = {
*/
processKeyDown: function (event) {

if (!this.enabled) { return; }

this.event = event;

if (this.isDown)
Expand Down Expand Up @@ -160,6 +170,8 @@ Phaser.Key.prototype = {
*/
processKeyUp: function (event) {

if (!this.enabled) { return; }

this.event = event;

if (this.isUp)
Expand Down Expand Up @@ -188,6 +200,7 @@ Phaser.Key.prototype = {
this.isUp = true;
this.timeUp = this.game.time.now;
this.duration = this.game.time.now - this.timeDown;
this.enabled = true;

this.onDown.removeAll();
this.onUp.removeAll();
Expand Down

0 comments on commit 01ccbd9

Please sign in to comment.