# Math Broblem

Get help using Construct 2

### » Tue Apr 08, 2014 5:34 pm

i need explanation for how next event should work:

There is gauge, with dipstick on middle on it, like car's speedometer. Well, this won't work like car one's.

I need event/fuction/etc, that makes on click dipstick go full 360 degrees spin in X seconds, where X is variable i want.

For example, like if X is 1, dipstick goes full 360 degrees spin in one second.
~~~~Jabutosama
B
14
S
3
Posts: 68
Reputation: 1,459

### » Tue Apr 08, 2014 7:43 pm

Try this:

Let's assume you want to rotate an object "Stick".

Give Stick a boolean: Stick.Rotating and set it to false.
Declare variable Stick.Rot_Time = x (where x is the time you want the stick to take to rotate 360 degrees).
Declare Stick.Start_Angle = Stick.Angle (the starting angle, in case it's not zero).

Then, when you want to rotate the Stick, set Stick.Rotating = True and use the following code:

Code: Select all
`If Stick.Rotating = True:>  Trigger once: Set Stick.Start_Angle to Stick_Angle>  Set Stick.Angle to Stick.Angle + (360 * dt / Stick.Rot_Time)  -- this will rotate clockwise, use a minus to rotate anti-clockwise>  If Stick.Angle is within 2 degrees of Stick.Start_angle  -- this stops the stick at 1 rotation**>->  Set Stick.Rotating to False>->  Set Stick.Angle to Stick.Start_Angle`

** It won't work if you want it to rotate in less than 1.5 seconds because that is 4 degrees per tick at 60 fps. If the rotation rate is any faster and/or the framerate drops then it could miss this check window and carry on spinning. If that happens, just use a wider window...

An alternative would be to have a look at the plugins - you could use Rex's Rotate To plugin for this as well.
Learning other engines, but C2 still rocks.
B
80
S
22
G
95
Posts: 2,422
Reputation: 57,681

### » Thu Apr 10, 2014 2:52 pm

thank you.
~~~~Jabutosama
B
14
S
3
Posts: 68
Reputation: 1,459

### » Thu Apr 10, 2014 3:24 pm

@Jabutosama, I just had a thought - a tidier way of managing which direction to rotate would be to leave the code as is and just use negative time values for Stick.Rot_Time if you want the Stick to rotate in an anti-clockwise direction. Can't believe I didn't think of that first go!
Learning other engines, but C2 still rocks.
B
80
S
22
G
95
Posts: 2,422
Reputation: 57,681