×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2012.741 | Released: 7 Feb 2012, 8:45am | What's New | Download Now

Clock

Features demonstrated:

Digital clock, with reformatted UI for different orientations.

Code:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
display.setStatusBar( display.HiddenStatusBar )
 
local clock = display.newGroup()
 
local background = display.newImage( "purple.png" )
clock:insert( background, true )
background.x = 160; background.y = 240
 
-- Set the rotation point to the center of the screen
clock:setReferencePoint( display.CenterReferencePoint )
 
-- Create dynamic textfields
-- Note: these are iOS/MacOS fonts. If building for Android, choose available system fonts, 
-- or use native.systemFont / native.systemFontBold
 
local hourField = display.newText( "", 0, 0, "Arial-BoldMT", 180 )
hourField:setTextColor( 255, 255, 255, 70 )
clock:insert( hourField, true )
hourField.x = 100; hourField.y = 90; hourField.rotation = -15
 
local minuteField = display.newText( "", 0, 0, "Arial-BoldMT", 180 )
minuteField:setTextColor( 255, 255, 255, 70 )
clock:insert( minuteField, true )
minuteField.x = 100; minuteField.y = 240; minuteField.rotation = -15
 
local secondField = display.newText( "", 0, 0, "Arial-BoldMT", 180 )
secondField:setTextColor( 255, 255, 255, 70 )
clock:insert( secondField, true )
secondField.x = 100; secondField.y = 390; secondField.rotation = -15
 
-- Create captions
local hourLabel = display.newText( "hours ", 0, 0, "Zapfino", 40 )
hourLabel:setTextColor( 131, 255, 131, 255 )
clock:insert( hourLabel, true )
hourLabel.x = 220; hourLabel.y = 100
 
local minuteLabel = display.newText( "minutes ", 0, 0, "Zapfino", 40 )
minuteLabel:setTextColor( 131, 255, 131, 255 )
clock:insert( minuteLabel, true )
minuteLabel.x = 220; minuteLabel.y = 250
 
local secondLabel = display.newText( "seconds ", 0, 0, "Zapfino", 40 )
secondLabel:setTextColor( 131, 255, 131, 255 )
clock:insert( secondLabel, true )
secondLabel.x = 210; secondLabel.y = 400
 
 
local function updateTime()
        local time = os.date("*t")
        
        local hourText = time.hour
        if (hourText < 10) then hourText = "0" .. hourText end
        hourField.text = hourText
        
        local minuteText = time.min
        if (minuteText < 10) then minuteText = "0" .. minuteText end
        minuteField.text = minuteText
        
        local secondText = time.sec
        if (secondText < 10) then secondText = "0" .. secondText end
        secondField.text = secondText
end
 
updateTime() -- run once on startup, so correct time displays immediately
 
 
-- Update the clock once per second
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )
 
 
-- Use accelerometer to rotate display automatically
local function onOrientationChange( event )
 
        -- Adapt text layout to current orientation     
        local direction = event.type
 
        if ( direction == "landscapeLeft" or direction == "landscapeRight" ) then
                hourField.y = 120
                secondField.y = 360
                hourLabel.y = 130
                secondLabel.y = 370
        elseif ( direction == "portrait" or direction == "portraitUpsideDown" ) then
                hourField.y = 90
                secondField.y = 390
                hourLabel.y = 100
                secondLabel.y = 400
        end
 
        -- Rotate clock so it remains upright
        local newAngle = clock.rotation - event.delta
        transition.to( clock, { time=150, rotation=newAngle } ) 
end
 
Runtime:addEventListener( "orientation", onOrientationChange )