Page 1 of 1
real time count-down
Posted: Tue Dec 10, 2013 8:12 pm
by pielago
okay here is my question is there a code i can have
that got to do with real time countdown?
i have one that starts at any minute to 0
but the sad part its that when it hits like (10 secs)
it goes to a single number
scene 1
example ( 2:10)
example ( 2: 9) <-- don't want that!
example ( 2: 8) <-- don't want that!
example ( 0: 0) <-- don't want that!
scene 2
example ( 2:10)
example ( 2:09)<---- i want that
example ( 2:08)<---- i want that
example ( 0:00)<---- i want that
scene 1 is what i have BUT scene 2 is what i seek for.. can someone help me?????
Re: real time count-down
Posted: Tue Dec 10, 2013 8:18 pm
by mickeyjm
You can use string.format when displaying the time to achieve this
For example:
Or, to format the minutes aswell:
Code: Select all
string.format("%02.0f:%02.0f",minutes,seconds)
Re: real time count-down
Posted: Tue Dec 10, 2013 8:22 pm
by pielago
?? how do i use that?
like can u give me an easy example?(code)
do i put that under load or update?
Re: real time count-down
Posted: Tue Dec 10, 2013 8:44 pm
by mickeyjm
You use this when you actually want to get a string representation of the timer.
For example if you are writing the timer in the top left corner of the screen you might do
Code: Select all
love.graphics.print(string.format("%02.0f:%02.0f",minutes,seconds),0,0) --Print formatted time at 0,0
Or if you are assigning the time to a variable for later use you might do
Code: Select all
local timerAsString = string.format("%02.0f:%02.0f",minutes,seconds)
Re: real time count-down
Posted: Tue Dec 10, 2013 9:06 pm
by Robin
Or, y'know, simply:
Code: Select all
local timerAsString = string.format("%02d:%02d",minutes,seconds)
Re: real time count-down
Posted: Wed Dec 11, 2013 8:41 am
by pielago
ugh can you do a .love with a simple countdown from 2:00 to 0:00
it will be a great help for me and also for who ever comes across this problem
Re: real time count-down
Posted: Wed Dec 11, 2013 9:19 am
by Plu
I think this works, but I cannot test it right now. (You can just put this as main.lua and run it)
Code: Select all
function love.load()
time = 120 -- initial value in seconds
end
function love.update( dt )
time = time - dt -- make it count down
if time < 0 then
time = 0 -- make it stop at 0 in the sloppiest way possible
end
end
function love.draw()
local minutes = math.floor( time / 60 ) -- calculate how many minutes are left
local seconds = math.floor( time % 60 ) -- calculate how many seconds are left
love.graphics.print( string.format("%02d:%02d",minutes,seconds), 400, 300 ) -- print it somewhere near the middle of the screen with proper formatting
end
Re: real time count-down
Posted: Wed Dec 11, 2013 4:47 pm
by pielago
it works i see thank you so much..
I need to learn more about TIME...