Mikoziq Posted June 24, 2015 Share Posted June 24, 2015 Hello Everyone, So... how should I refactor the code to gain my tank ability to move reverse ? Should i use else if statement in those lines ?if (cursors.left.isDown) { tank.angle -= 4; } else if (cursors.right.isDown) { tank.angle += 4; } if (cursors.up.isDown) { // The speed we'll travel at currentSpeed = 300; } if (cursors.down.isDown) ??? { //something } else { if (currentSpeed > 0) { currentSpeed -= 4; } } Link to comment Share on other sites More sharing options...
Snade Posted June 25, 2015 Share Posted June 25, 2015 If you put in a number thats below zero it should move backwards if I am right.if (cursors.up.isDown){ // The speed we'll travel at currentSpeed = 300;}if (cursors.down.isDown){ // The speed we'll travel at currentSpeed = -300;} Link to comment Share on other sites More sharing options...
Skeptron Posted June 25, 2015 Share Posted June 25, 2015 You should use else if statements for opposite directions : if(goUp)else if(goDown) // if(goRight) else if(goLeft). Other than that you want to be able to go left AND up, or right AND down for example, so no else if between these directions. Link to comment Share on other sites More sharing options...
Janzen Posted January 21, 2016 Share Posted January 21, 2016 I'm having the same issue. I already tried what Snade and Skeptron suggested: if (cursors.up.isDown){ // The speed we'll travel at currentSpeed = 300;} else if (cursors.down.isDown){ // The speed we'll travel at currentSpeed = -300;} Pressing the down arrow still does nothing. Seems simple enough, but I'm new to phaser. What am I missing here? Does it have to do with this? if (currentSpeed > 0) { game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity); } Link to comment Share on other sites More sharing options...
Janzen Posted January 21, 2016 Share Posted January 21, 2016 I figured out that currentSpeed can't be negative. velocityFromRotation doesn't move the tank when currentSpeed is negative. Not sure why. Can I make it work somehow? Link to comment Share on other sites More sharing options...
Janzen Posted January 21, 2016 Share Posted January 21, 2016 Solved this by using P2 physics and the moveForward and moveBackward variables. Link to comment Share on other sites More sharing options...
gratis_reckon Posted February 8, 2016 Share Posted February 8, 2016 if (cursors.up.isDown) { if (currentSpeed >= 300) { currentSpeed = 300; } else { currentSpeed += 20; } } else if (cursors.down.isDown) { if (currentSpeed <= -300) { currentSpeed = -300; } else { currentSpeed -= 20; } } else { if (currentSpeed > 10) { currentSpeed -= 10; } else if (currentSpeed < -10) { currentSpeed += 10; } else { currentSpeed = 0; } } game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity); -------------------------- This worked for me. Link to comment Share on other sites More sharing options...
Recommended Posts