# Toggle move left and right every second click on the same button?

I have a sprite

When you first press the left mouse button/touch, my sprite moves to the left.
When you hold down the left mouse button/touch the second time, my sprite moves to the right.
When you hold down the left mouse button/touch the third time, my sprite moves to the left.
When you hold down the left mouse button/touch the fourth time, my sprite moves to the right.
And so on…
I want to swap the move of my sprite in X every time it’s hold down.

I have tested to create a global variabel thats hold 0 or 1 and check if it’s 1 or 1 and move depending on that.

and what is the question?

Me too

never mind. here is my version.

The variable doesn’t need to be global. You can read about the variables on the wiki:
http://wiki.compilgames.net/doku.php/gdevelop5/all-features/variables

First toggle the value of direction variable by multiplying the value by -1.

Make sure the default value of direction = 1

At the beginning of the scene
–>Do = 1 to Scene variable Direction

Then toggle the value by multiplying by -1

If Touch or Left mouse button is down
Trigger Once (IMPORTANT)
–>Do *-1 to scene variable Direction

So the idea is 1*-1 = -1 and -1*-1 = 1 and so you toggle the value of Direction this way between -1 and 1.

Then move spaceship based on the value of Direction.

If Scene variable Direction = 1
–>Do +4 to X position of SpaceShip
If Scene variable Direction = -1
–>Do -4 to X position of SpaceShip

1 Like

I always learn from you Master!

Thank You!