Whole number expressions

For questions about using Classic.

Post » Wed May 21, 2008 2:41 pm

Is there a way to force Construct to round up an expression to the nearest whole number, rather than having a decimal number result? I'm moving an object around using loops and timedelta in order to keep things smooth. An example is in the deceleration event:

['Right' key isn't down] [Value 'X Value' is greater than 0]

= [Player: Set 'X Value' to 'Player.Value('X Value')+(Player.Value('Deceleration')*timedelta)

This subtracts a set deceleration value from the players X Value, and a loop is then run that moves the player according to the value of 'X Value'. The deceleration event should continue to subtract from 'X Value' until the value reaches 0 and the object comes to a stop.

The trouble is, due to using the timedelta expression to maintain a smooth framerate, the deceleration value comes out as a decimal value, and so it sometimes doesnt reach the 0 value, meaning my object keeps moving a tiny amount even after it is supposed to have decelerated to a stop.

Basically, is there some sort of expression I can use to force Construct to round up the 'X Value' to a whole number to ensure the value will eventually reach 0 through deceleration?

Hope that makes sense,

Cheers, Tom
B
2
S
1
G
5
Posts: 50
Reputation: 1,500

Post » Wed May 21, 2008 2:52 pm

the sys espression " Round"
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed May 21, 2008 2:53 pm

Use round(number) in the expression editor. There's also expressions for rounding up and down rather than rounding to the closest, but I don't remember what they are - they're in the math section of the system object.
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Wed May 21, 2008 4:35 pm

You could also add the subevent:

+ 'X Value' is less than 0
-> Set 'X Value' to 0

Or simply use the expression:
Player: Set 'X Value' to: max(Player.Value('X Value')+(Player.Value('Deceleration')*timedelta), 0)

Then you get the smooth floating point subtraction which never falls below 0. For small 'Deceleration' values, rounding may cause the movement to stop or become jittery, so you should prefer one of these methods.
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578

Post » Thu May 22, 2008 2:27 am

The 'max' function works nicely, now everything runs smoothly whilst still subtraction to zero. Thanks!

- Tom
B
2
S
1
G
5
Posts: 50
Reputation: 1,500


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest