rfzahid Posted October 24, 2015 Share Posted October 24, 2015 Hello guys, I am a newbie and still learning Phaser.js framework and want to clarify a doubt. 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? Link to comment Share on other sites More sharing options...
bruno_ Posted October 24, 2015 Share Posted October 24, 2015 Can you control just one player?If so, have you set maxInputPointers? Link to comment Share on other sites More sharing options...
chongdashu Posted October 24, 2015 Share Posted October 24, 2015 Perhaps post some of your existing code so we can see where things might not be working correctly Link to comment Share on other sites More sharing options...
rfzahid Posted October 29, 2015 Author Share Posted October 29, 2015 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 More sharing options...
afdsfasdfsda Posted October 29, 2015 Share Posted October 29, 2015 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 More sharing options...
chongdashu Posted October 29, 2015 Share Posted October 29, 2015 So at first glance, all looks fine. What exactly is the problem you are having? Here is a jsFiddle that uses what you're doing shows that it should work fine.http://jsfiddle.net/chongdashu/5sbdvcd7/ Maybe post more of your code? Are you doing all the above in update()? Link to comment Share on other sites More sharing options...
rfzahid Posted October 29, 2015 Author Share Posted October 29, 2015 Hey its working finally I had some parts of code cluttered so it was difficult to find out the mistake.I formatted the code and found out some lines of code were not at the right place.Thanks for the help Link to comment Share on other sites More sharing options...
chongdashu Posted October 29, 2015 Share Posted October 29, 2015 Great! It is probably a good idea to mark the thread as answered. Link to comment Share on other sites More sharing options...
Recommended Posts