Jump to content

2 players playing from same keyboard(WASD && arrow keys)


rfzahid
 Share

Recommended Posts

Hello guys, I am a newbie and still learning Phaser.js framework and want to clarify a doubt. :mellow:

 

I am trying to make a game where two players can play together from the same keyboard to fight it out. :)

One from WASD keys and another from default arrow keys.

I have created two players and binded them to their respective keys, but I am not able to control players from their respective keys :(

 

How can I achieve such? :blink:

Link to comment
Share on other sites

cursor1 = game.input.keyboard.createCursorKeys();

//cursor2 key settings...

upButton = game.input.keyboard.addKey(Phaser.Keyboard.W);

downButton = game.input.keyboard.addKey(Phaser.Keyboard.S);

leftButton = game.input.keyboard.addKey(Phaser.Keyboard.A);

rightButton = game.input.keyboard.addKey(Phaser.Keyboard.D);

// Player1 controls...

if (cursor1.left.isDown) {

player1.body.velocity.x = -150;

player1.animations.play('left');

} else if (cursor1.right.isDown) {

player1.body.velocity.x = 150;

player1.animations.play('right');

} else {

player1.animations.stop();

player1.frame = 4;

}

if (cursor1.up.isDown && player1.body.touching.down) {

player1.body.velocity.y = -350;

}

if (!player1.body.touching.down) {

if (cursor1.left.isDown) {

player1.frame = 3;

} else if (cursor1.right.isDown) {

player1.frame = 6;

}

}

// Player2 controls...

if (leftButton.isDown) {

player2.body.velocity.x = -150;

player2.animations.play('left');

} else if (rightButton.isDown) {

player2.body.velocity.x = 150;

player2.animations.play('right');

} else {

player2.animations.stop();

player2.frame = 4;

}

if (upButton.isDown && player2.body.touching.down) {

player2.body.velocity.y = -350;

}

if (!player2.body.touching.down) {

if (leftButton.isDown) {

player2.frame = 3;

} else if (rightButton.isDown) {

player2.frame = 6;

}

}

Link to comment
Share on other sites

function create(){    up = game.input.keyboard.addKey(Phaser.Keyboard.UP); //and so on    //or...    cursorKeys = game.input.keyboard.createCursorKeys();    //and for the WASD    w = game.input.keyboard.addKey(Phaser.Keyboard.W);}function update(){    if (up.isDown /*and if you created the cursorKeys*/cursorKeys.up.isDown)    {        player1.body.velocity.y = -100; /*or*/         player1.y -= 10;    }    if (w.isDown)    {        player2.body.velocity.y = -100; /*or*/         player2.y -= 10;    }    //and so on...}

Try something like this

 

EDIT

 

I don't see why your code doesn't work, try remaking it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...