Sep 142011
 

Learn how to display a timer counting upwards in seconds and minutes in your iPhone or Android application.

Many apps will give a user a score based on their time and in these apps it only makes sense to have a visible counter on screen.

Sure, you could simply start at 0 and keep on counting up forever, but minutes and seconds just look nicer.

The code below is copy and paste, so whack it in a main.lua file to see how it works in the simulator!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
display.setStatusBar(display.HiddenStatusBar)

local bg = display.newRect( 0, 0, 320, 480 )

local secsText = 00
local minsText = 0

local timeText = display.newText(minsText.. ":0" ..secsText, 40, 40, "Helvetica", 24)
timeText:setTextColor(0,0,0)

local function updateTime (event)
secsText = secsText + 1

if secsText < 10 then secsText = "0" ..secsText elseif secsText > 59 then
secsText = 00
minsText = minsText+1
end
timeText.text = minsText .. ":" ..secsText
end
timer.performWithDelay(1000, updateTime, 0)

It’s that simple!

Running that you will see a white background and mins:secs display counting up once per second. If you’d like to see how it will look at much higher times/numbers, change;

1
timer.performWithDelay(1000, updateTime, 0)

to;

1
timer.performWithDelay(100, updateTime, 0)

or even;

1
timer.performWithDelay(10, updateTime, 0)

Just note that you will want to give the timer a name, eg; myTimer = timer.performWithDelay() so that you can pause and resume (if you’re using Corona version .602 or above) and cancel it when the level ends, using timer.cancel(myTimer).

Simple, functional and likely to come in handy for lots of you!

Enjoy!

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!

 

  6 Responses to “Display a Timer – Minutes and Seconds”

  1. @jomo

    just add this add on more zero to the

    Before it’s this local minsText = 0

    Change to

    local minsText = 00

    That will fix it.

    ng

  2. The Timer works! :)

    Just one more thing.
    I don’t understand why, but at some moments, the Timer removes one “0″ from the hours.

    Like this:
    -00:00
    -0:01 ?
    -01:00

    So strange. :s

  3. Hello Peach and thank you for this great tutorial ! :)

    I tried to make the same principle for Hours and Minutes. But my code doesn’t work at the moment.
    Any ideas ?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    local hoursText = 00
    local minutesText = 00
    timeText = display.newText("0" ..hoursText.. ":0" ..minutesText, 80, 8, "Helvetica", 10 )
    timeText:setTextColor(255, 255, 255)

    local function updateTime (event)
    minutesText = minutesText + 1
    if (minutesText < 10) then
    minutesText = "0" ..minutesText
    elseif (minutesText > 59) then
    hoursText = hoursText + 1
    minutesText = 00
    elseif (hoursText < 10) then
    hoursText = "0" ..hoursText
    elseif (hoursText > 23) then
    hoursText = 00
    minutesText = 00
    end
    timeText.text = hoursText.. ":" ..minutesText
    end

    timer.performWithDelay(10, updateTime, 0)
  4. Good but m try to find the complete tutorial to develop a simple application using corona and lua if you know then plese send the address on my id Thanks But this is a great effort that you define the tutorial and usage of different functions or etc.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.