Best approach for displaying and saving a "level timer"

Hi All,

In my current project, I’d like to have a timer that runs on each level of my platformer style game.

Since the game is meant to play fairly fast, I’d like the timer to display 10ths or even 100ths of a second.

Do you recommend that I use a timer.performWithDelay() function to update a timer function or should I create a function that uses an enterFrame listener and does some math with the system.getTimer() function?

I’ve been experimenting with these approaches and have encountered a few weird things. Using a delay of 10 on timer.performWithDelay(), I don’t seem to get the 100ths of a second that I would expect. Eyballing it against my stop watch it seems to run at about half that speed (two seconds to count 100). It’s not a deal breaker if my “seconds” aren’t quite really seconds but it seemed odd.

Also, what would be the best way to get these times to display in a stopwatch style format (mm:ss:th)? Is there a way to concatenate the values with colons at the correct places and display them all as text?

Is there a way to force “0s” onto the front of a number value? If I start at time “0” it just displays as “0” and I need to sort that out as “00:00:00” or “00:00.0”. If I can figure out how to concatenate strings with the correct digits of a number then I guess I can sort the logic on how to make it display a “0” for any missing places.

I’d like to save the last time and best time for each level a player plays. Save values using JSON?

Thanks in advance for any help. I thought this was going to be a straightforward task but I’ve managed to make it much more confusing than I expected :slight_smile:

[import]uid: 105707 topic_id: 36627 reply_id: 336627[/import]

Look at the api call: string.format().

print(string.format("%02d:%02d:%02d",hour,min,sec) will print:

01:02:03

Keep in mind you’re app runs at either 30 frames per second (0.03333th of a second) or 60 frames per second (0.166667th of a second).

So you’re not going to get updates any faster than that.

Rob [import]uid: 199310 topic_id: 36627 reply_id: 144935[/import]

Hi @EHO,
By the nature an app running at a certain number of frames per second, and with timers executing, etc., you’re not likely going to get a perfect timer running that fast (100ths of seconds, even 10ths), and matching your stopwatch. For example, let’s say you try to get 100th of a second…

1 second = 1000 ms … 1000 / 100 = 10 ms

And even if your game is running at 60 FPS (not 30) that works out to:

1 second = 1000 ms … 1000 / 60 = 16.66666777 ms

So a timer might execute in the “next game cycle” but it likely won’t mesh up perfectly with your stopwatch.

I suggest you read this tutorial on working with time, if you haven’t already:
http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

As for formatting strings, there are some examples in the tutorial, and you can experiment further using the “string.format()” function:
http://docs.coronalabs.com/api/library/string/format.html

Hope this helps (a little),
Brent [import]uid: 200026 topic_id: 36627 reply_id: 144938[/import]

Brent and Rob,

I appreciate your tips, these are very helpful.

Great point about the frame rate dictating what’s possible as far as the smallest portions of a second are concerned.

I’m not looking for exact per second accuracy, but I do want the numbers to appear fluid as they update (polish consideration) and I’d like to get at least 10ths of a second displayed to create a sense of tension against the clock running up.

The example and link to the string information is exceptionally helpful.

Thanks so much :slight_smile: [import]uid: 105707 topic_id: 36627 reply_id: 144946[/import]

Look at the api call: string.format().

print(string.format("%02d:%02d:%02d",hour,min,sec) will print:

01:02:03

Keep in mind you’re app runs at either 30 frames per second (0.03333th of a second) or 60 frames per second (0.166667th of a second).

So you’re not going to get updates any faster than that.

Rob [import]uid: 199310 topic_id: 36627 reply_id: 144935[/import]

Hi @EHO,
By the nature an app running at a certain number of frames per second, and with timers executing, etc., you’re not likely going to get a perfect timer running that fast (100ths of seconds, even 10ths), and matching your stopwatch. For example, let’s say you try to get 100th of a second…

1 second = 1000 ms … 1000 / 100 = 10 ms

And even if your game is running at 60 FPS (not 30) that works out to:

1 second = 1000 ms … 1000 / 60 = 16.66666777 ms

So a timer might execute in the “next game cycle” but it likely won’t mesh up perfectly with your stopwatch.

I suggest you read this tutorial on working with time, if you haven’t already:
http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

As for formatting strings, there are some examples in the tutorial, and you can experiment further using the “string.format()” function:
http://docs.coronalabs.com/api/library/string/format.html

Hope this helps (a little),
Brent [import]uid: 200026 topic_id: 36627 reply_id: 144938[/import]

Brent and Rob,

I appreciate your tips, these are very helpful.

Great point about the frame rate dictating what’s possible as far as the smallest portions of a second are concerned.

I’m not looking for exact per second accuracy, but I do want the numbers to appear fluid as they update (polish consideration) and I’d like to get at least 10ths of a second displayed to create a sense of tension against the clock running up.

The example and link to the string information is exceptionally helpful.

Thanks so much :slight_smile: [import]uid: 105707 topic_id: 36627 reply_id: 144946[/import]

Look at the api call: string.format().

print(string.format("%02d:%02d:%02d",hour,min,sec) will print:

01:02:03

Keep in mind you’re app runs at either 30 frames per second (0.03333th of a second) or 60 frames per second (0.166667th of a second).

So you’re not going to get updates any faster than that.

Rob [import]uid: 199310 topic_id: 36627 reply_id: 144935[/import]

Hi @EHO,
By the nature an app running at a certain number of frames per second, and with timers executing, etc., you’re not likely going to get a perfect timer running that fast (100ths of seconds, even 10ths), and matching your stopwatch. For example, let’s say you try to get 100th of a second…

1 second = 1000 ms … 1000 / 100 = 10 ms

And even if your game is running at 60 FPS (not 30) that works out to:

1 second = 1000 ms … 1000 / 60 = 16.66666777 ms

So a timer might execute in the “next game cycle” but it likely won’t mesh up perfectly with your stopwatch.

I suggest you read this tutorial on working with time, if you haven’t already:
http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

As for formatting strings, there are some examples in the tutorial, and you can experiment further using the “string.format()” function:
http://docs.coronalabs.com/api/library/string/format.html

Hope this helps (a little),
Brent [import]uid: 200026 topic_id: 36627 reply_id: 144938[/import]

Brent and Rob,

I appreciate your tips, these are very helpful.

Great point about the frame rate dictating what’s possible as far as the smallest portions of a second are concerned.

I’m not looking for exact per second accuracy, but I do want the numbers to appear fluid as they update (polish consideration) and I’d like to get at least 10ths of a second displayed to create a sense of tension against the clock running up.

The example and link to the string information is exceptionally helpful.

Thanks so much :slight_smile: [import]uid: 105707 topic_id: 36627 reply_id: 144946[/import]

Look at the api call: string.format().

print(string.format("%02d:%02d:%02d",hour,min,sec) will print:

01:02:03

Keep in mind you’re app runs at either 30 frames per second (0.03333th of a second) or 60 frames per second (0.166667th of a second).

So you’re not going to get updates any faster than that.

Rob [import]uid: 199310 topic_id: 36627 reply_id: 144935[/import]

Hi @EHO,
By the nature an app running at a certain number of frames per second, and with timers executing, etc., you’re not likely going to get a perfect timer running that fast (100ths of seconds, even 10ths), and matching your stopwatch. For example, let’s say you try to get 100th of a second…

1 second = 1000 ms … 1000 / 100 = 10 ms

And even if your game is running at 60 FPS (not 30) that works out to:

1 second = 1000 ms … 1000 / 60 = 16.66666777 ms

So a timer might execute in the “next game cycle” but it likely won’t mesh up perfectly with your stopwatch.

I suggest you read this tutorial on working with time, if you haven’t already:
http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

As for formatting strings, there are some examples in the tutorial, and you can experiment further using the “string.format()” function:
http://docs.coronalabs.com/api/library/string/format.html

Hope this helps (a little),
Brent [import]uid: 200026 topic_id: 36627 reply_id: 144938[/import]

Brent and Rob,

I appreciate your tips, these are very helpful.

Great point about the frame rate dictating what’s possible as far as the smallest portions of a second are concerned.

I’m not looking for exact per second accuracy, but I do want the numbers to appear fluid as they update (polish consideration) and I’d like to get at least 10ths of a second displayed to create a sense of tension against the clock running up.

The example and link to the string information is exceptionally helpful.

Thanks so much :slight_smile: [import]uid: 105707 topic_id: 36627 reply_id: 144946[/import]