Working with Time

Hello everyone

Maybe this would be something good to include in the code snippets area.

Could anyone point me to some examples explaining how to measure time in Corona?

Looking for a simple way to measure the time that passed from the time a sprite is displayed, to the time the sprite is touched. Would also like to display a timer on the screen.

I’m sure this has been done before but I’m brand new to Corona so not even sure what keywords to search the API for!

Thanks for any pointers. [import]uid: 191855 topic_id: 33223 reply_id: 333223[/import]

Time in corona isn’t that difficult. You just need to understand a few things.

First, many timers are in the “millisecond” resolution. When you use timer.performWithDelay, transition.to, any of the storyboard transitions, the times are all specified in milliseconds.

The display on the other hand only updates either 30 times per second or 60 times per second based on your settings, so the fastest screen updates you get are in the order of 33 ms or 16ms. You will never be able to show anything. In other words if you have a timer displaying and it shows 100ms even though you can count to 110ms, you won’t get the next screen update until its 116 ms (or 133ms)

Some things are second resolution, including the os.time() call which returns you the time in seconds since 1-jan-1970 Midnight UTC. Obviously that’s useless for doing more precise measurements, but it’s great for date math, seeding random number generators, calculating time for second resolution count down/up clocks, etc.

Most events that fire have a time value with them which is in millisecond resolution that you can use too for event driven actions.

But the gem you’re looking for is the API call system.getTimer() http://docs.coronalabs.com/api/library/system/getTimer.html

This timer returns you the time in milliseconds since your app started. When you show your sprite, you can set a variable:

spriteStart = system.getTimer()  

then in your event listener for the touch event;

 local now - system.getTimer()  
 local deltaTime = now - spriteStart  

then you have the recorded time of the touch that you can use to restart your counter:

[code]
sprintStart = now

If you need to do that.
[import]uid: 19626 topic_id: 33223 reply_id: 132081[/import]

Time in corona isn’t that difficult. You just need to understand a few things.

First, many timers are in the “millisecond” resolution. When you use timer.performWithDelay, transition.to, any of the storyboard transitions, the times are all specified in milliseconds.

The display on the other hand only updates either 30 times per second or 60 times per second based on your settings, so the fastest screen updates you get are in the order of 33 ms or 16ms. You will never be able to show anything. In other words if you have a timer displaying and it shows 100ms even though you can count to 110ms, you won’t get the next screen update until its 116 ms (or 133ms)

Some things are second resolution, including the os.time() call which returns you the time in seconds since 1-jan-1970 Midnight UTC. Obviously that’s useless for doing more precise measurements, but it’s great for date math, seeding random number generators, calculating time for second resolution count down/up clocks, etc.

Most events that fire have a time value with them which is in millisecond resolution that you can use too for event driven actions.

But the gem you’re looking for is the API call system.getTimer() http://docs.coronalabs.com/api/library/system/getTimer.html

This timer returns you the time in milliseconds since your app started. When you show your sprite, you can set a variable:

spriteStart = system.getTimer()  

then in your event listener for the touch event;

 local now - system.getTimer()  
 local deltaTime = now - spriteStart  

then you have the recorded time of the touch that you can use to restart your counter:

[code]
sprintStart = now

If you need to do that.
[import]uid: 19626 topic_id: 33223 reply_id: 132081[/import]