Jump to content

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


Mikoziq
 Share

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;        }    }
Link to comment
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;}
Link to comment
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.

Link to comment
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)
    {
        game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
    }

 

Link to comment
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;
                }
            }

            game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);

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

This worked for me.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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