Skip to content

Commit 719b81c

Browse files
committed
GamepadButton.justPressed and justReleased now correctly report if the button has just been pressed or released (thanks @padpadpad phaserjs#1019)
1 parent 9047e4d commit 719b81c

3 files changed

Lines changed: 23 additions & 33 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Version 2.1.0 - "Cairhien" - -in development-
147147
* Tilemap.hasTile now checks the Tile.index value and will return false if the index is -1 (i.e. a non-active tile) (thanks @elgansayer #859)
148148
* Sound.restart used to cause the Sound to double-up if it was already playing when called. Now correctly stops the sound before restarting it (thanks @wombatbuddy #1136)
149149
* GamePad axis detection now works again properly in Firefox (#1035)
150+
* GamepadButton.justPressed and justReleased now correctly report if the button has just been pressed or released (thanks @padpadpad #1019)
150151

151152
### p2.js 0.6.0 Changes and New Features
152153

src/input/Gamepad.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ Object.defineProperty(Phaser.Gamepad.prototype, "supported", {
511511
/**
512512
* How many live gamepads are currently connected.
513513
* @name Phaser.Gamepad#padsConnected
514-
* @property {boolean} padsConnected - How many live gamepads are currently connected.
514+
* @property {number} padsConnected - How many live gamepads are currently connected.
515515
* @readonly
516516
*/
517517
Object.defineProperty(Phaser.Gamepad.prototype, "padsConnected", {
@@ -525,7 +525,7 @@ Object.defineProperty(Phaser.Gamepad.prototype, "padsConnected", {
525525
/**
526526
* Gamepad #1
527527
* @name Phaser.Gamepad#pad1
528-
* @property {boolean} pad1 - Gamepad #1;
528+
* @property {Phaser.SinglePad} pad1 - Gamepad #1;
529529
* @readonly
530530
*/
531531
Object.defineProperty(Phaser.Gamepad.prototype, "pad1", {
@@ -539,7 +539,7 @@ Object.defineProperty(Phaser.Gamepad.prototype, "pad1", {
539539
/**
540540
* Gamepad #2
541541
* @name Phaser.Gamepad#pad2
542-
* @property {boolean} pad2 - Gamepad #2
542+
* @property {Phaser.SinglePad} pad2 - Gamepad #2
543543
* @readonly
544544
*/
545545
Object.defineProperty(Phaser.Gamepad.prototype, "pad2", {
@@ -553,7 +553,7 @@ Object.defineProperty(Phaser.Gamepad.prototype, "pad2", {
553553
/**
554554
* Gamepad #3
555555
* @name Phaser.Gamepad#pad3
556-
* @property {boolean} pad3 - Gamepad #3
556+
* @property {Phaser.SinglePad} pad3 - Gamepad #3
557557
* @readonly
558558
*/
559559
Object.defineProperty(Phaser.Gamepad.prototype, "pad3", {
@@ -567,7 +567,7 @@ Object.defineProperty(Phaser.Gamepad.prototype, "pad3", {
567567
/**
568568
* Gamepad #4
569569
* @name Phaser.Gamepad#pad4
570-
* @property {boolean} pad4 - Gamepad #4
570+
* @property {Phaser.SinglePad} pad4 - Gamepad #4
571571
* @readonly
572572
*/
573573
Object.defineProperty(Phaser.Gamepad.prototype, "pad4", {

src/input/GamepadButton.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,14 @@ Phaser.GamepadButton.prototype = {
101101
*/
102102
processButtonDown: function (value) {
103103

104-
if (this.isDown)
105-
{
106-
this.duration = this.game.time.now - this.timeDown;
107-
this.repeats++;
108-
}
109-
else
110-
{
111-
this.isDown = true;
112-
this.isUp = false;
113-
this.timeDown = this.game.time.now;
114-
this.duration = 0;
115-
this.repeats = 0;
116-
this.value = value;
117-
118-
this.onDown.dispatch(this, value);
119-
}
104+
this.isDown = true;
105+
this.isUp = false;
106+
this.timeDown = this.game.time.now;
107+
this.duration = 0;
108+
this.repeats = 0;
109+
this.value = value;
110+
111+
this.onDown.dispatch(this, value);
120112

121113
},
122114

@@ -129,15 +121,12 @@ Phaser.GamepadButton.prototype = {
129121
*/
130122
processButtonUp: function (value) {
131123

132-
if (this.isDown)
133-
{
134-
this.isDown = false;
135-
this.isUp = true;
136-
this.timeUp = this.game.time.now;
137-
this.value = value;
124+
this.isDown = false;
125+
this.isUp = true;
126+
this.timeUp = this.game.time.now;
127+
this.value = value;
138128

139-
this.onUp.dispatch(this, value);
140-
}
129+
this.onUp.dispatch(this, value);
141130

142131
},
143132

@@ -165,9 +154,9 @@ Phaser.GamepadButton.prototype = {
165154
*/
166155
justPressed: function (duration) {
167156

168-
if (typeof duration === "undefined") { duration = 250; }
157+
duration = duration || 250;
169158

170-
return (this.isDown && this.duration < duration);
159+
return (this.isDown === true && (this.timeDown + duration) > this.game.time.now);
171160

172161
},
173162

@@ -180,9 +169,9 @@ Phaser.GamepadButton.prototype = {
180169
*/
181170
justReleased: function (duration) {
182171

183-
if (typeof duration === "undefined") { duration = 250; }
172+
duration = duration || 250;
184173

185-
return (this.isDown === false && (this.game.time.now - this.timeUp < duration));
174+
return (this.isUp === true && (this.timeUp + duration) > this.game.time.now);
186175

187176
},
188177

0 commit comments

Comments
 (0)