fedora Posted October 4, 2015 Share Posted October 4, 2015 I am building a game using a Tile Map. The Map loads and all appears fine. I can move the player right and left with no issues. Unfortunately, I have been unable to get movement up and down. I have scoured web with no success. I am not looking for 'Pac Man" auto movement. I want the player to move on keyboard input. Here is the relevant sections of code:this.physics.arcade.gravity.y = 200; // The player and its settings player = this.add.sprite(200, this.world.height - 250, 'dave'); // We need to enable physics on the player this.physics.arcade.enable(player); // Player physics properties. player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; // Our animations of walking player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); player.animations.add('up', [0, 1, 2, 3], 10, true); player.animations.add('down', [5, 6, 7, 8], 10, true); // Keyboard Controls cursors = this.input.keyboard.createCursorKeys();// Reset the players velocity (movement) player.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { // Move to the right player.body.velocity.x = 150; player.animations.play('right'); } else if (cursors.up.isDown) { // Move to the Up player.body.velocity.y = 150; player.animations.play('up'); } else if (cursors.down.isDown) { // Move to the Down player.body.velocity.y = 150; player.animations.play('down'); }Any guidance would be greatly appreciated. Link to comment Share on other sites More sharing options...
henrikkee Posted October 4, 2015 Share Posted October 4, 2015 Well i don't know why this is happening, but i find one more error.else if (cursors.up.isDown){player.body.velocity.y = 150;}else if (cursors.down.isDown){player.body.velocity.y = 150;}Both are y= 150, so both are going down Link to comment Share on other sites More sharing options...
drhayes Posted October 5, 2015 Share Posted October 5, 2015 You should try putting the X and Y movement in their own if statements; i.e. don't do if(left) else if (right) *else* if (up)... separate those bad boys. Try getting rid of gravity entirely, as well, see if you get movement up (after you fix the 150/-150 issue from the above post). Link to comment Share on other sites More sharing options...
fedora Posted October 5, 2015 Author Share Posted October 5, 2015 Thank you both for your replies. I have resolved the issue by making the below changes: //this.physics.arcade.gravity.x = 200; // Player physics properties. Give the little guy a slight bounce. //player.body.bounce.y = 0.2; //player.body.gravity.y = 300; //player.body.collideWorldBounds = true; else if (cursors.up.isDown) { // Move to the Up player.body.velocity.y = -150; player.animations.play('up'); } else if (cursors.down.isDown) { // Move to the Down player.body.velocity.y = 150; player.animations.play('down'); }Per your suggestion, drhayes -- I will also be breaking out x and y movement into their own if statements Link to comment Share on other sites More sharing options...
Recommended Posts