* Fixed issue causing Keyboard.justPressed to always fire (thanks stemkoski)

* Added Keyboard.addKey() which creates a new Phaser.Key object that can be polled for updates, pressed states, etc. See the 2 new examples showing use.
This commit is contained in:
Richard Davey
2013-10-01 01:18:29 +01:00
parent fa1ed04aa8
commit 8668b82ef6
28 changed files with 397 additions and 23 deletions
+1
View File
@@ -361,6 +361,7 @@ Phaser.Input.prototype = {
if (this.pointer10) { this.pointer10.update(); }
this._pollCounter = 0;
},
/**
+142
View File
@@ -0,0 +1,142 @@
Phaser.Key = function (game, keycode) {
this.game = game;
/**
*
* @property isDown
* @type Boolean
**/
this.isDown = false;
/**
*
* @property isUp
* @type Boolean
**/
this.isUp = false;
/**
*
* @property altKey
* @type Boolean
**/
this.altKey = false;
/**
*
* @property ctrlKey
* @type Boolean
**/
this.ctrlKey = false;
/**
*
* @property shiftKey
* @type Boolean
**/
this.shiftKey = false;
/**
*
* @property timeDown
* @type Number
**/
this.timeDown = 0;
/**
*
* @property duration
* @type Number
**/
this.duration = 0;
/**
*
* @property timeUp
* @type Number
**/
this.timeUp = 0;
/**
*
* @property repeats
* @type Number
**/
this.repeats = 0;
this.keyCode = keycode;
this.onDown = new Phaser.Signal();
this.onUp = new Phaser.Signal();
};
Phaser.Key.prototype = {
/**
*
* @method update
* @param {KeyboardEvent} event.
* @return {}
*/
processKeyDown: function (event) {
this.altKey = event.altKey;
this.ctrlKey = event.ctrlKey;
this.shiftKey = event.shiftKey;
if (this.isDown)
{
// Key was already held down, this must be a repeat rate based event
this.duration = event.timeStamp - this.timeDown;
this.repeats++;
}
else
{
this.isDown = true;
this.isUp = false;
this.timeDown = event.timeStamp;
this.duration = 0;
this.repeats = 0;
this.onDown.dispatch(this);
}
},
processKeyUp: function (event) {
this.isDown = false;
this.isUp = true;
this.timeUp = event.timeStamp;
this.onUp.dispatch(this);
},
/**
* @param {Number} [duration]
* @return {bool}
*/
justPressed: function (duration) {
if (typeof duration === "undefined") { duration = 250; }
return (this.isDown && this.duration < duration);
},
/**
* @param {Number} [duration]
* @return {bool}
*/
justReleased: function (duration) {
if (typeof duration === "undefined") { duration = 250; }
return (this.isDown == false && (this.game.time.now - this.timeUp < duration));
}
};
+77 -22
View File
@@ -2,7 +2,15 @@ Phaser.Keyboard = function (game) {
this.game = game;
this._keys = {};
this._hotkeys = {};
this._capture = {};
this.callbackContext = this;
this.onDownCallback = null;
this.onUpCallback = null;
this.onDown = new Phaser.Signal();
this.onUp = new Phaser.Signal();
};
@@ -19,16 +27,41 @@ Phaser.Keyboard.prototype = {
_onKeyDown: null,
_onKeyUp: null,
addCallbacks: function (context, onDown, onUp) {
this.callbackContext = context;
this.onDownCallback = onDown;
if (typeof onUp !== 'undefined')
{
this.onUpCallback = onUp;
}
},
addKey: function (keycode) {
this._hotkeys[keycode] = new Phaser.Key(this.game, keycode);
return this._hotkeys[keycode];
},
removeKey: function (keycode) {
delete (this._hotkeys[keycode]);
},
start: function () {
var _this = this;
this._onKeyDown = function (event) {
return _this.onKeyDown(event);
return _this.processKeyDown(event);
};
this._onKeyUp = function (event) {
return _this.onKeyUp(event);
return _this.processKeyUp(event);
};
document.body.addEventListener('keydown', this._onKeyDown, false);
@@ -80,10 +113,11 @@ Phaser.Keyboard.prototype = {
},
/**
* @param {KeyboardEvent} event
*/
onKeyDown: function (event) {
processKeyDown: function (event) {
if (this.game.input.disabled || this.disabled)
{
@@ -95,18 +129,40 @@ Phaser.Keyboard.prototype = {
event.preventDefault();
}
if (!this._keys[event.keyCode])
if (this.onDownCallback)
{
this._keys[event.keyCode] = {
isDown: true,
timeDown: this.game.time.now,
timeUp: 0
};
this.onDownCallback.call(this.callbackContext, event);
}
if (this._keys[event.keyCode] && this._keys[event.keyCode].isDown)
{
// Key already down and still down, so update
this._keys[event.keyCode].duration = this.game.time.now - this._keys[event.keyCode].timeDown;
}
else
{
this._keys[event.keyCode].isDown = true;
this._keys[event.keyCode].timeDown = this.game.time.now;
if (!this._keys[event.keyCode])
{
// Not used this key before, so register it
this._keys[event.keyCode] = {
isDown: true,
timeDown: this.game.time.now,
timeUp: 0,
duration: 0
};
}
else
{
// Key used before but freshly down
this._keys[event.keyCode].isDown = true;
this._keys[event.keyCode].timeDown = this.game.time.now;
this._keys[event.keyCode].duration = 0;
}
}
if (this._hotkeys[event.keyCode])
{
this._hotkeys[event.keyCode].processKeyDown(event);
}
},
@@ -114,7 +170,7 @@ Phaser.Keyboard.prototype = {
/**
* @param {KeyboardEvent} event
*/
onKeyUp: function (event) {
processKeyUp: function (event) {
if (this.game.input.disabled || this.disabled)
{
@@ -126,20 +182,19 @@ Phaser.Keyboard.prototype = {
event.preventDefault();
}
if (!this._keys[event.keyCode])
if (this.onUpCallback)
{
this._keys[event.keyCode] = {
isDown: false,
timeDown: 0,
timeUp: this.game.time.now
};
this.onUpCallback.call(this.callbackContext, event);
}
else
if (this._hotkeys[event.keyCode])
{
this._keys[event.keyCode].isDown = false;
this._keys[event.keyCode].timeUp = this.game.time.now;
this._hotkeys[event.keyCode].processKeyUp(event);
}
this._keys[event.keyCode].isDown = false;
this._keys[event.keyCode].timeUp = this.game.time.now;
},
reset: function () {
@@ -160,7 +215,7 @@ Phaser.Keyboard.prototype = {
if (typeof duration === "undefined") { duration = 250; }
if (this._keys[keycode] && this._keys[keycode].isDown === true && (this.game.time.now - this._keys[keycode].timeDown < duration))
if (this._keys[keycode] && this._keys[keycode].isDown && this._keys[keycode].duration < duration)
{
return true;
}