* 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

View file

@ -65,6 +65,9 @@ Version 1.0.7 (in progress in the dev branch)
* Added Canvas.setUserSelect() to disable touchCallouts and user selections within the canvas.
* When the game boots it will now by default disable user-select and touch action events on the game canvas.
* Loaded.setPreloadSprite now rounds the width/height values and starts from 1. This fixes canvas draw errors in IE9/10 and Firefox.
* 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.

View file

@ -1,7 +1,7 @@
/**
* Phaser - http://www.phaser.io
*
* v1.0.7 - Built at: Mon, 30 Sep 2013 18:13:41 +0000
* v1.0.7 - Built at: Mon, 30 Sep 2013 21:50:07 +0000
*
* @author Richard Davey http://www.photonstorm.com @photonstorm
*

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

67
examples/input/key.php Normal file
View file

@ -0,0 +1,67 @@
<?php
$title = "Creating and using a Key object";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
}
var sprite;
var upKey;
var downKey;
var leftKey;
var rightKey;
function create() {
game.stage.backgroundColor = '#736357';
sprite = game.add.sprite(300, 300, 'phaser');
// In this example we'll create 4 specific keys (up, down, left, right) and monitor them in our update function
upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
}
function update() {
if (upKey.isDown)
{
sprite.y--;
}
else if (downKey.isDown)
{
sprite.y++;
}
if (leftKey.isDown)
{
sprite.x--;
}
else if (rightKey.isDown)
{
sprite.x++;
}
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,58 @@
<?php
$title = "Keyboard Hotkeys";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.image('logo', 'assets/sprites/phaser_tiny.png');
game.load.image('pineapple', 'assets/sprites/pineapple.png');
}
var key1;
var key2;
var key3;
function create() {
game.stage.backgroundColor = '#736357';
// Here we create 3 hotkeys, keys 1-3 and bind them all to their own functions
key1 = game.input.keyboard.addKey(Phaser.Keyboard.ONE);
key1.onDown.add(addPhaserDude, this);
key2 = game.input.keyboard.addKey(Phaser.Keyboard.TWO);
key2.onDown.add(addPhaserLogo, this);
key3 = game.input.keyboard.addKey(Phaser.Keyboard.THREE);
key3.onDown.add(addPineapple, this);
}
function addPhaserDude () {
game.add.sprite(game.world.randomX, game.world.randomY, 'phaser');
}
function addPhaserLogo () {
game.add.sprite(game.world.randomX, game.world.randomY, 'logo');
}
function addPineapple () {
game.add.sprite(game.world.randomX, game.world.randomY, 'pineapple');
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,47 @@
<?php
$title = "Keyboard justPressed";
require('../head.php');
?>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
var phaser;
function preload() {
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
}
function create() {
game.stage.backgroundColor = '#736357';
phaser = game.add.sprite(300, 300, 'phaser');
}
function update() {
if (game.input.keyboard.justPressed(Phaser.Keyboard.UP))
{
phaser.y--;
}
if (game.input.keyboard.justPressed(Phaser.Keyboard.DOWN))
{
phaser.y++;
}
}
})();
</script>
<?php
require('../foot.php');
?>

View file

@ -52,6 +52,7 @@
<script src="../src/core/Game.js"></script>
<script src="../src/input/Input.js"></script>
<script src="../src/input/Key.js"></script>
<script src="../src/input/Keyboard.js"></script>
<script src="../src/input/Mouse.js"></script>
<script src="../src/input/MSPointer.js"></script>

View file

@ -361,6 +361,7 @@ Phaser.Input.prototype = {
if (this.pointer10) { this.pointer10.update(); }
this._pollCounter = 0;
},
/**

142
src/input/Key.js Normal file
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));
}
};

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