I made a function that fires when a button is pressed on the gamepad, but I can’t get another function to fire when the player releases the button on the gamepad. How can I do this?
There are indeed only 2 GamePad events,So, I use GamePad event like this.
import { __private, _decorator, Component, EventGamepad, Input, input, Label, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('GamePadTest')
export class GamePadTest extends Component {
private label:Label;
start() {
input.on(Input.EventType.GAMEPAD_INPUT, this.onGamePadInput, this)
input.on(Input.EventType.GAMEPAD_CHANGE, this.onGamePadChange, this)
}
private device0: __private._pal_input__GamepadInputDevice;
protected update(deltaTime: number) {
if (this.device0 && this.device0.connected) {
if (this.device0.dpad.left.getValue()) {
this.left();
}
}
}
public onGamePadInput(e: EventGamepad) {
console.log("input");
}
public onGamePadChange(e: EventGamepad) {
console.log("gamepad change id:" + e.gamepad.deviceId);
this.device0 = e.gamepad;
}
public left() {
console.log("pressing left");
}
}