I am new in corona .I am making a puzzle game so
I need a Stop watch in this app so please help me how to make it . [import]uid: 107104 topic_id: 30702 reply_id: 330702[/import]
There are multiple ways to attack this depending on the resolution of your clock. For instance in my Trivia Game, I have a count down timer in seconds. But a true stop watch tends to have a resolution in 1/100ths of a second.
If you just need second resolution, that’s easy enough to do with a timer:
local seconds = 0
local timeDisplay = display.newText(tostring(seconds),0,0,"Helvetica",32)
local function updateClock(event)
seconds = seconds + 1
timeDisplay.text = tostring(seconds)
end
stopWatch = timer.performWithDelay(1000,updateClock,0) -- loop the timer endlessly
Of course you probably want the time in hh:mm:ss format, so you will need to learn about os.date() and the various formatting parameters so you probably would do:
local function updateClock(event)
seconds = seconds + 1
timeDisplay.text = os.date("%H:%M:%S",seconds)
end
Now if you want higher resolution, you probably will want to use an “enterFrame” Runtime listener along with system.getTimer(). Now this method will update the time every 1/30th of a second or every 1/60th of a second which is as fast as you can update the screen, but system.getTimer() has a millionth of second resolution:
local seconds = 0
local startTime = system.getTimer()
local timeDisplay = display.newText(tostring(seconds),0,0,"Helvetica",32)
local function updateClock(event)
seconds = system.getTimer() - startTime / 1000
local trimmedSeconds = math.floor(seconds)
local thousandths = seconds - trimmedSeconds
timeDisplay.text = os.date("%H:%M:%S",trimmedSeconds) .. "." string.format("%.2f",thousandths)
end
Runtime:addEventListener("enterFrame",updateClock)
[import]uid: 19626 topic_id: 30702 reply_id: 122998[/import]
There are multiple ways to attack this depending on the resolution of your clock. For instance in my Trivia Game, I have a count down timer in seconds. But a true stop watch tends to have a resolution in 1/100ths of a second.
If you just need second resolution, that’s easy enough to do with a timer:
local seconds = 0
local timeDisplay = display.newText(tostring(seconds),0,0,"Helvetica",32)
local function updateClock(event)
seconds = seconds + 1
timeDisplay.text = tostring(seconds)
end
stopWatch = timer.performWithDelay(1000,updateClock,0) -- loop the timer endlessly
Of course you probably want the time in hh:mm:ss format, so you will need to learn about os.date() and the various formatting parameters so you probably would do:
local function updateClock(event)
seconds = seconds + 1
timeDisplay.text = os.date("%H:%M:%S",seconds)
end
Now if you want higher resolution, you probably will want to use an “enterFrame” Runtime listener along with system.getTimer(). Now this method will update the time every 1/30th of a second or every 1/60th of a second which is as fast as you can update the screen, but system.getTimer() has a millionth of second resolution:
local seconds = 0
local startTime = system.getTimer()
local timeDisplay = display.newText(tostring(seconds),0,0,"Helvetica",32)
local function updateClock(event)
seconds = system.getTimer() - startTime / 1000
local trimmedSeconds = math.floor(seconds)
local thousandths = seconds - trimmedSeconds
timeDisplay.text = os.date("%H:%M:%S",trimmedSeconds) .. "." string.format("%.2f",thousandths)
end
Runtime:addEventListener("enterFrame",updateClock)
[import]uid: 19626 topic_id: 30702 reply_id: 122998[/import]
Hi Rob,
I tried the last code but not sure why it starts always with 04:00:00.
I want to make stop watch to start when scene did show triggers in composer at 00:00:00 then it increment by seconds…
When the game character finish the quiz the the watch should stop… the final time will be the acheived score …
In the next level… I am thinking to continue from the last time and adds on it…
After 3rd level… the count should stop with final score at this time and game is done for that quiz…
Please help…
Regards
Abdul
i am wondering why the following
[lua]
timeDisplay.text = os.date("%H:%M:%S",seconds)
[/lua]
gives 04:00:00 when starts not 00:00:00 … is it because timezoene ?
how can I make it to start at 00:00:00
Thanks
Abdul
can someone help me in this … I did many research still not understand why … -_-
Regards
Abdul
I would not use os.date() for this functionality. It’s trying too take your seconds value and base it from the Unix time epoch of Jan 1, 1970 at 0:00 GMT. I would keep track of your timer seconds and then simply do some math to get the hour, min and sec values:
local hours = math.floor(seconds/3600)
local minutes = math.floor( (seconds - hours * 3600 ) / 60 )
local sec = seconds - (hours * 3600) - (minutes * 60)
local timeString = string.format("%02d:%02d:%02d",hours, minutes, sec)
or something like that. Your timer is not a date value, just a counter. Using os.date() is making it do things you don’t want it to do.
Rob
Super Rob
Thanks man , you are right , i should do counter instead of the date function… i appreciate your hint…
Regards
Abdulaziz
Hi Rob,
I tried the last code but not sure why it starts always with 04:00:00.
I want to make stop watch to start when scene did show triggers in composer at 00:00:00 then it increment by seconds…
When the game character finish the quiz the the watch should stop… the final time will be the acheived score …
In the next level… I am thinking to continue from the last time and adds on it…
After 3rd level… the count should stop with final score at this time and game is done for that quiz…
Please help…
Regards
Abdul
i am wondering why the following
[lua]
timeDisplay.text = os.date("%H:%M:%S",seconds)
[/lua]
gives 04:00:00 when starts not 00:00:00 … is it because timezoene ?
how can I make it to start at 00:00:00
Thanks
Abdul
can someone help me in this … I did many research still not understand why … -_-
Regards
Abdul
I would not use os.date() for this functionality. It’s trying too take your seconds value and base it from the Unix time epoch of Jan 1, 1970 at 0:00 GMT. I would keep track of your timer seconds and then simply do some math to get the hour, min and sec values:
local hours = math.floor(seconds/3600)
local minutes = math.floor( (seconds - hours * 3600 ) / 60 )
local sec = seconds - (hours * 3600) - (minutes * 60)
local timeString = string.format("%02d:%02d:%02d",hours, minutes, sec)
or something like that. Your timer is not a date value, just a counter. Using os.date() is making it do things you don’t want it to do.
Rob
Super Rob
Thanks man , you are right , i should do counter instead of the date function… i appreciate your hint…
Regards
Abdulaziz