how do you turn seconds into minutes and seconds

» Wed Nov 04, 2009 5:21 pm

Hello everyone,

I am making a timer for a game, but at the moment it can only be in seconds (i.e. 100 seconds). Does anyone know how I can turn 100 seconds into 1 min 40 secs. Is this possible using events or will I have to use a piece of python code?

B
4
G
3
Posts: 25
Reputation: 975

» Wed Nov 04, 2009 6:18 pm

It's easily possible in events! You just need a little math. If seconds is your number in seconds, floor(seconds / 60) is the minutes (divide by 60 and round down), and seconds % 60 is the seconds counter (modulo 60, ie. remainder after dividing by 60).
Scirra Founder
B
362
S
216
G
75
Posts: 23,067
Reputation: 180,321

» Wed Nov 04, 2009 7:03 pm

I made a example for you.
Press the A key to start the timer.
It is ascending up to infinite or in this case all the number reach 0:00 and count up again.
B
21
S
4
G
4
Posts: 280
Reputation: 2,934

» Thu Nov 05, 2009 1:09 pm

thanks toralord and Ashley,

Everybody states (quite rightly) how good construct is, and for me as a newbe to gamemaking it is the only platform I would attempt to write the game i am making at the moment. But the other great thing about construct is how supportive everyone is. I have asked three questions so far and I have never had to wait more than two hours for an answer. People like me are really grateful for the help!

thanks again.
B
4
G
3
Posts: 25
Reputation: 975

» Tue Nov 24, 2009 7:05 am

[quote="Ashley":1ds9taky]It's easily possible in events! You just need a little math. If seconds is your number in seconds, floor(seconds / 60) is the minutes (divide by 60 and round down), and seconds % 60 is the seconds counter (modulo 60, ie. remainder after dividing by 60).[/quote:1ds9taky]

what if you have above 59 minutes and you want hours too?
B
135
S
65
G
17
Posts: 1,766
Reputation: 19,727

» Tue Nov 24, 2009 9:41 am

[quote="alspal":2unqdj5p][quote="Ashley":2unqdj5p]It's easily possible in events! You just need a little math. If seconds is your number in seconds, floor(seconds / 60) is the minutes (divide by 60 and round down), and seconds % 60 is the seconds counter (modulo 60, ie. remainder after dividing by 60).[/quote:2unqdj5p]

what if you have above 59 minutes and you want hours too?[/quote:2unqdj5p]

hours: floor(seconds / 3600) (or: seconds / 60 / 60)
then use the result of seconds modulo 3600 for the rest of the calculations as seen above

totalseconds = 3980
hours = floor(totalseconds / 3600) = 1
totalseconds = totalseconds % 3600 = 380
minutes = floor(totalseconds / 60) = 6
seconds = totalseconds % 60 = 20

"01:06:20"

yeah, nice

EDIT: I forgot, in case someone asks for days too, just start with floor-dividing by 86400 (=3600 * 24) and use totalseconds % 86400 for the rest of the calculations.

Now try to find yourself the right value for weeks
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

» Tue Nov 24, 2009 11:32 am

Cheers!
Also, I'd never noticed that zeropad system expression before - that's really useful!
B
135
S
65
G
17
Posts: 1,766
Reputation: 19,727