Value of Variable not affecting Sprite

I have a variable set to stop/start a Sprite rotating
And a second variable to start/stop the Sprite bullet movement.

The isRotating variable isn’t stopping the rotation. However-
The isMoving variable does work to toggle movement on and off.

When I touch the screen I want the Sprite to move and rotate. When I touch again I want the Sprite to stop rotating and stop moving.

Only the movement variable is working.

I’m using values 0 and 1 as booleans. One is to turn it on and 0 is to turn it off.

I’ve also got two text box that shows the number of the variables. The numbers change like they’re supposed to but only one behavior is affected

Any ideas what could be wrong?

1 Like

I just tried coding that and had the same problem.
I coded “System.runtime every frame, if VAR =1,turn clockwise 2”
When sprite senses touch down, if VAR = 0, set VAR to 1
When sprite senses touch down, if VAR = 1, set VAR to 0

I tested Touchup, Pressed, Clicked, CLick down, CLick up.
The sprite wouldn’t stop turning.

Maybe if you make some sort of delay when changing the variables value.
So the variable only changes a tenth of a second after press down.

1 Like

@Infinitum3D and @Suzzy-Grey . Try using different values for “Runtime” and maybe sometimes use the timer. As practice shows, after the last update we can solve many complex problems and the logic of the work looks correct. Perhaps in some cases you may have problems with this, but otherwise everything should work correctly.

@Infinitum3D - Please send a link to your project, I’ll try to help you implement the logic for the your project…

1 Like

Hello @Infinitum3D, @Suzzy-Grey, @Ready and everyone,

This problem of avoiding a race condition is an interesting one! (for me, anyhow :wink: )

There surely are multiple successful approaches but I would like to share a solution I found using button states and a semaphore. The approach works like this: we can exploit button states (touch down, touch up) to detect sequential events within a single frame. On button down, we claim the semaphore (aka lock) that allows us to change the rotation state of our game object on the following button up event.

In the demo I’ve included extra variables to visualize the internal state, but in a game they can be omitted.

I hope this is helpful!


Thank you @auntiel! It looks interesting enough!:+1: I hope this will really help for our subscribers!