easierbycode

Members
  • Content Count

    1
  • Joined

  • Last visited

  1. easierbycode

    What's the best way to handle two player movement with gamepads.

    I'm also starting on a two player shooter with gamepad support. Here's the approach I took: (shortened for brevity) class Player extends Phaser.Sprite { gamepad; constructor( gamepad, game, x = 0, y = 0, key = 'dude' ) { super( game, x, y, key ); game.add.existing( this ); game.physics.arcade.enable( this ); this.gamepad = gamepad; } } class ExampleState extends Phaser.State { gamepad1 = null; gamepad2 = null; create() { this.game.input.gamepad.start(); this.gamepad1 = this.game.input.gamepad.pad1; this.gamepad2 = this.game.input.gamepad.pad2; [ this.gamepad1, this.gamepad2 ].forEach(( gamepad ) => { gamepad.addCallbacks( gamepad, { onConnect: () => { let newPlayer = new Player( gamepad, this.game ); this.game.players.add( newPlayer ); } }); }); } update() { this.game.players.forEachAlive(( player ) => { let gamepad = player.gamepad; player.body.velocity.x = 0; player.body.velocity.y = 0; if ( gamepad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.1 ) { player.body.velocity.x = -150; } else if ( gamepad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.1 ) { player.body.velocity.x = 150; } if ( gamepad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.1 ) { player.body.velocity.y = -150; } else if ( gamepad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.1 ) { player.body.velocity.y = 150; } }); } } example-state.js player.js