# [Phaser Tanks] - How to move... Backwards ?

## Recommended Posts

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;        }    }`
##### Share on other sites

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;}`
##### Share on other sites

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.

##### Share on other sites

• 6 months later...

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)
{
}``````

##### Share on other sites

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?

##### Share on other sites

Solved this by using P2 physics and the moveForward and moveBackward variables.

##### Share on other sites

• 3 weeks later...
``````
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;
}
}

``````

--------------------------

This worked for me.