From e4054cb644db8d5af6465185fec232cb103a84ef Mon Sep 17 00:00:00 2001 From: CryonautLex Date: Tue, 29 Apr 2025 16:36:40 -0400 Subject: [PATCH] Fix: prevent gamepad buttons from sending bogus down events on init --- src/input/gamepad/Button.js | 5 +++-- src/input/gamepad/Gamepad.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/input/gamepad/Button.js b/src/input/gamepad/Button.js index 41ddf79ca0..edd0f650f3 100644 --- a/src/input/gamepad/Button.js +++ b/src/input/gamepad/Button.js @@ -19,12 +19,13 @@ var Events = require('./events'); * * @param {Phaser.Input.Gamepad.Gamepad} pad - A reference to the Gamepad that this Button belongs to. * @param {number} index - The index of this Button. + * @param {boolean} isPressed - Whether or not the button is already being pressed at creation time. This prevents the Button from emitting spurious 'down' events at first update. */ var Button = new Class({ initialize: - function Button (pad, index) + function Button (pad, index, isPressed) { /** * A reference to the Gamepad that this Button belongs to. @@ -82,7 +83,7 @@ var Button = new Class({ * @default false * @since 3.0.0 */ - this.pressed = false; + this.pressed = isPressed; }, /** diff --git a/src/input/gamepad/Gamepad.js b/src/input/gamepad/Gamepad.js index 3c7f64649d..5450336c5e 100644 --- a/src/input/gamepad/Gamepad.js +++ b/src/input/gamepad/Gamepad.js @@ -83,7 +83,7 @@ var Gamepad = new Class({ for (var i = 0; i < pad.buttons.length; i++) { - buttons.push(new Button(this, i)); + buttons.push(new Button(this, i, (pad.buttons[i].value >= 0.5))); } /**