diff --git a/README.md b/README.md index c414119e..5126296a 100644 --- a/README.md +++ b/README.md @@ -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. + diff --git a/build/phaser.js b/build/phaser.js index 2c1f1254..27401015 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -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 * diff --git a/examples/assets/skies/cavern1.png b/examples/assets/skies/cavern1.png new file mode 100644 index 00000000..9002fae2 Binary files /dev/null and b/examples/assets/skies/cavern1.png differ diff --git a/examples/assets/skies/cavern2.png b/examples/assets/skies/cavern2.png new file mode 100644 index 00000000..c20943de Binary files /dev/null and b/examples/assets/skies/cavern2.png differ diff --git a/examples/assets/skies/chrome.png b/examples/assets/skies/chrome.png new file mode 100644 index 00000000..414f578e Binary files /dev/null and b/examples/assets/skies/chrome.png differ diff --git a/examples/assets/skies/fire.png b/examples/assets/skies/fire.png new file mode 100644 index 00000000..759fac2b Binary files /dev/null and b/examples/assets/skies/fire.png differ diff --git a/examples/assets/skies/fog.png b/examples/assets/skies/fog.png new file mode 100644 index 00000000..76c7386c Binary files /dev/null and b/examples/assets/skies/fog.png differ diff --git a/examples/assets/skies/sky1.png b/examples/assets/skies/sky1.png new file mode 100644 index 00000000..18a30759 Binary files /dev/null and b/examples/assets/skies/sky1.png differ diff --git a/examples/assets/skies/sky2.png b/examples/assets/skies/sky2.png new file mode 100644 index 00000000..10a85c76 Binary files /dev/null and b/examples/assets/skies/sky2.png differ diff --git a/examples/assets/skies/sky3.png b/examples/assets/skies/sky3.png new file mode 100644 index 00000000..26714faf Binary files /dev/null and b/examples/assets/skies/sky3.png differ diff --git a/examples/assets/skies/sky4.png b/examples/assets/skies/sky4.png new file mode 100644 index 00000000..a2989c3e Binary files /dev/null and b/examples/assets/skies/sky4.png differ diff --git a/examples/assets/skies/sky5.png b/examples/assets/skies/sky5.png new file mode 100644 index 00000000..58db4343 Binary files /dev/null and b/examples/assets/skies/sky5.png differ diff --git a/examples/assets/skies/space1.png b/examples/assets/skies/space1.png new file mode 100644 index 00000000..9d7643f1 Binary files /dev/null and b/examples/assets/skies/space1.png differ diff --git a/examples/assets/skies/space2.png b/examples/assets/skies/space2.png new file mode 100644 index 00000000..2198cd4d Binary files /dev/null and b/examples/assets/skies/space2.png differ diff --git a/examples/assets/skies/sunorbit.png b/examples/assets/skies/sunorbit.png new file mode 100644 index 00000000..8ccc1305 Binary files /dev/null and b/examples/assets/skies/sunorbit.png differ diff --git a/examples/assets/skies/sunset.png b/examples/assets/skies/sunset.png new file mode 100644 index 00000000..18f5212b Binary files /dev/null and b/examples/assets/skies/sunset.png differ diff --git a/examples/assets/skies/toxic.png b/examples/assets/skies/toxic.png new file mode 100644 index 00000000..8bfb378d Binary files /dev/null and b/examples/assets/skies/toxic.png differ diff --git a/examples/assets/skies/underwater1.png b/examples/assets/skies/underwater1.png new file mode 100644 index 00000000..c1ee9900 Binary files /dev/null and b/examples/assets/skies/underwater1.png differ diff --git a/examples/assets/skies/underwater2.png b/examples/assets/skies/underwater2.png new file mode 100644 index 00000000..0af1808a Binary files /dev/null and b/examples/assets/skies/underwater2.png differ diff --git a/examples/assets/skies/underwater3.png b/examples/assets/skies/underwater3.png new file mode 100644 index 00000000..d4ff8955 Binary files /dev/null and b/examples/assets/skies/underwater3.png differ diff --git a/examples/assets/skies/wtf.png b/examples/assets/skies/wtf.png new file mode 100644 index 00000000..35159e26 Binary files /dev/null and b/examples/assets/skies/wtf.png differ diff --git a/examples/input/key.php b/examples/input/key.php new file mode 100644 index 00000000..3a1ed307 --- /dev/null +++ b/examples/input/key.php @@ -0,0 +1,67 @@ + + + + + \ No newline at end of file diff --git a/examples/input/keyboard hotkeys.php b/examples/input/keyboard hotkeys.php new file mode 100644 index 00000000..94e162f6 --- /dev/null +++ b/examples/input/keyboard hotkeys.php @@ -0,0 +1,58 @@ + + + + + \ No newline at end of file diff --git a/examples/input/keyboard justpressed.php b/examples/input/keyboard justpressed.php new file mode 100644 index 00000000..afddd675 --- /dev/null +++ b/examples/input/keyboard justpressed.php @@ -0,0 +1,47 @@ + + + + + \ No newline at end of file diff --git a/examples/js.php b/examples/js.php index bbf12080..32e1a272 100644 --- a/examples/js.php +++ b/examples/js.php @@ -52,6 +52,7 @@ + diff --git a/src/input/Input.js b/src/input/Input.js index f9df0d7a..a4075192 100644 --- a/src/input/Input.js +++ b/src/input/Input.js @@ -361,6 +361,7 @@ Phaser.Input.prototype = { if (this.pointer10) { this.pointer10.update(); } this._pollCounter = 0; + }, /** diff --git a/src/input/Key.js b/src/input/Key.js new file mode 100644 index 00000000..519b4e23 --- /dev/null +++ b/src/input/Key.js @@ -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)); + + } + +}; \ No newline at end of file diff --git a/src/input/Keyboard.js b/src/input/Keyboard.js index 1096a9a1..c19ad892 100644 --- a/src/input/Keyboard.js +++ b/src/input/Keyboard.js @@ -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; }