Dec 072011
 

A simple tutorial and sample showing how to make a countdown app. (This one is Christmas themed.)

The below is plug and play code – run it and you will see how many days, hours and minutes left until Christmas day.

countdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

--Create a new rectangle for the background, color it red
local background = display.newRect( 0, 0, 320, 480 )
background:setFillColor( 255, 80, 80 )

--The number of days remaining
local dayText = display.newText( "", 0, 0, native.systemFontBold, 160 )
dayText:setTextColor( 0, 0, 0, 200 )
dayText.x, dayText.y = 110, 90

--The number of hours remaining
local hourText = display.newText( "", 0, 0, native.systemFontBold, 160 )
hourText:setTextColor( 0, 0, 0, 120 )
hourText.x, hourText.y = 110, 240

--The number of minutes remaining
local minutesText = display.newText( "", 0, 0, native.systemFontBold, 160 )
minutesText:setTextColor( 0, 0, 0, 65 )
minutesText.x, minutesText.y = 110, 390

-- Create labels to indicate what the numbers mean
local dayLabel = display.newText( "days", 0, 0, native.systemFont, 40 )
dayLabel:setTextColor( 255, 255, 255 )
dayLabel.x = 220; dayLabel.y = 100

local hourLabel = display.newText( "hours", 0, 0, native.systemFont, 40 )
hourLabel:setTextColor( 255, 255, 255 )
hourLabel.x = 220; hourLabel.y = 250

local minuteLabel = display.newText( "minutes", 0, 0, native.systemFont, 40 )
minuteLabel:setTextColor( 255, 255, 255 )
minuteLabel.x = 210; minuteLabel.y = 400

--Function to update the countdown clock
local function updateTime()
local time = os.date("*t")

local daysLeft = 358 - time.yday
if (daysLeft < 10) then
daysLeft = "0" .. daysLeft
end
dayText.text = daysLeft

local hoursLeft = 23 - time.hour
if (hoursLeft < 10) then
hoursLeft = "0" .. hoursLeft
end
hourText.text = hoursLeft

local minutesLeft = 60 - time.min
if (minutesLeft < 10) then
miunteText = "0" .. minutesLeft
end
minutesText.text = minutesLeft

end

--Update the time once immediately to display the correct time
updateTime()

-- Update the clock once per second
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

Enjoy!

…. and stay tuned. I’ll be (hopefully) posting a little Christmas video to in the not too distant future so you’ll actually get to see and hear me, which will be weird.

I’m also hoping to get some details ironed out and put up some info about next years Techority 48 Hour Challenge in the next week.

Peach Pellen :)

 

Like this post? Subscribe to the monthly newsletter!

 

  9 Responses to “Making A Countdown App With Corona SDK”

  1. Hi P.

    There is a typo in a line of the code. It says miunte instead of minute.

    local minutesLeft = 60 – time.min
    if (minutesLeft < 10) then
    miunteText = "0" .. minutesLeft
    end

    A.

  2. Hey Chris,

    I like that idea – something I will definitely have to consider, thanks :)

    I need to try and go through my stuff at some point and find something really gentle to optimize – I have a rough rule where I try not to introduce more than one new concept (however small) per tutorial.

    This could end up being a good one to start on though :)

  3. Hehe yeah I made also something like that when I started programming :D

    Maybe it would be good to expand the tutorials – showing how it can be done and how it can be optimized!
    I think that would be something everybody could benefit from :)
    Still a great job! Keep it up :D

  4. Hello :D

    Please do not feel stupid; it is not obvious at all that Christmas is the 358th day of the year, I should have been much clearer about that.

    My apologies :)
    Peach

  5. Hello my best friend!

    Thank you very much for the explanation at all! I understood it after get your tips about that 358 number. :)

    PS: (I feel a little stupid after all. hehe)

    Cheers,
    Rodrigo.

  6. Hey Chris,

    Heh, yes, there are things that could have been done to make the above a bit better – but as it’s a newbie tutorial and this still works, I wanted to make it as simple as possible. (This is very close to what one of my first Corona apps looked like.)

    Peach :)

  7. Nice little Tutorial ;)

    I think you could add more snippets to it to make the code more useful:

    1
    local hours = string.format("%02i", time.hour)

    This will give every integer a leading zero if it has only 1 digit!

    Also there are better ways to determine the dateDiff, but that’s another topic! :)

    Chris

  8. Hello my friend!

    I’m glad you enjoyed the post :)

    I understand what you are asking me – you don’t understand what “358″ means, because a year (except a leap year) has 365 days.

    The reason is because this app is counting down to Christmas Day, which is the 358th day of the year :) (I should probably have mentioned that in the notes, hehe, sorry!)

    One of these days I promise I’ll find time to email you – if you saw my inbox you would understand why I am sometimes so slow ;)

    Peach :)

  9. Hello Mrs.!! :)

    Thank you for posting one more awesome “code” or “app” already!

    PS: I just did not understand well the “why” did you use the number 358 for the variable “daysLeft” although the year has 365 days. :)

    PS: Please…go and tell me that I do not make sense… :)

    My best wishes as always for you my friend. ;)

    Rodrigo Costa.

 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.