I am trying to set up gamepad controls for my game. I have done this previously in Phaser 2, but now I am working in Phaser 3 and there don’t seem to be a lot of resources on gamepad controls. I managed to code D-pad controls for left-right motion, but I need to also use the other buttons for actions…
Has anyone done this before?
I used parts of this tutorial:
any help or resources would be greatly appreciated
I’m pretty sure, the XBox controller and Playstation controller have different mapping keys. You will have to handle the keys for the controller your using and the other one in case others would use it. I managed to map buttons from an old phone numpad, I’m sure you will be able to figure this out.
I’m currently using a SNES controller clone which uses Xbox button mappings (if I recall correctly). My game will mostly be used for outreach by the same group people, using this controller, so that button mapping would be preferred.
Have you got any sources or code snippets which outline how to map buttons? I would really appreciate any info, as I’m not particularly advanced in game development by any means.
I figured out how to do this in the update() function:
update() {
let pad = Phaser.Input.Gamepad.Gamepad;
if (this.input.gamepad.total){
pad = this.input.gamepad.getPad(0);
}
if (pad.B){
// console.log('From Instructions 1 to Level 1');
this.scene.start('level1');
// console.log("Stopping current Scene");
this.scene.stop();
}
if (pad.A){
// console.log('From Instructions 1 to Title');
this.scene.start('gameTitle');
// console.log("Stopping current Scene");
this.scene.stop();
}
}
I’m currently figuring out how to make this work in the create() function, as I have some event emitters which are tied to key presses and I want to make them compatible with the controller.
Currently trying to adapt this to work with the D-pad on the controller:
this.input.keyboard.on('keydown', event => {
switch (event.key) {
case 'ArrowLeft':
this.activeSelection -= 1;
this.events.emit('CHANGE_BUTTON');
break;
case 'ArrowRight':
this.activeSelection += 1;
this.events.emit('CHANGE_BUTTON');
break;
case 'ArrowDown':
this.activeSelection = 3;
this.events.emit('CHANGE_BUTTON');
break;
case 'ArrowUp':
this.activeSelection = 1;
this.events.emit('CHANGE_BUTTON');
break;
case 'Enter':
this.events.emit('SELECTITEM');
break;
}
});