Gamepad.on is not a function

Hiya! I’m trying to map a controller to be used in my game. I’m currently trying to make it work with my menu system, which can usually be navigated using WASD, arrow keys and the enter key. I want to add the d-pad and the B button to that list.

I ran into a problem where the .on() function is not recognised. I checked the documentation again and the function is there:
https://photonstorm.github.io/phaser3-docs/Phaser.Input.Gamepad.Gamepad.html#on__anchor

Here’s the error that I get
image

And here is the code that is associated with that error:

let pad = Phaser.Input.Gamepad.Gamepad;

	if (this.input.gamepad.total){
  		pad = this.input.gamepad.getPad(0);
	}

    

    pad.on('buttonDown', pad =>{
        switch (pad) {
            case up:
                this.activeText -= 1;
                this.events.emit('CHANGE_BUTTON');
                break;
            case down:
                this.activeText += 1;
                this.events.emit('CHANGE_BUTTON');
                break;
            case B:
                this.events.emit('SELECT');
                break;
        }
    });

Please take a look and feel free to give me any advice on how to fix it :slight_smile: Thank you!

You’re setting a variable equal to a class, but not constructing the class. Try
let pad = this.input.gamepad;

1 Like

Thanks! That got rid of the error, but the functionality isn’t there. I’m probably not using the switch statement right here.

Ususally you can assign what happens when a button is pressed using an if statement like so:

if (pad.up){
	//code here...
}

Is there a way to adapt this to a switch statement?

I don’t know if this will help you, but this is how I got numpad to work, I use flags, I also have an inGame flag.

//global variables
let previousInputState = {
left: 0,
right: 0,
up: 0,
down: 0,
ok: 0,
back: 0
}

let inputState = {
left: 0,
right: 0,
up: 0,
down: 0,
ok: 0,
back: 0
}

//my if statement
if (inputState.up == 1 && previousInputState.up == 0){//do whatever};
//reset
previousInputState.up = inputState.up;

my key handling JS file:
HandleKeys.js (8.9 KB)