Even modified sample clock freezes after about an hour running on my ipod touch.
Seems to be some problem with
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )
after about 3600 calls no more calls are made to updateTime. But orientation still works.
Below is modified sample with os.time performed only once…
Any feedback welcome??
– Abstract: Orientation sample app, with digital clock
– …comment stuff…
– Copyright © 2010 ANSCA Inc. All Rights Reserved.
– 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
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 = 240; hourLabel.y = 100
local minuteLabel = display.newText( "minutes ", 0, 0, “Zapfino”, 40 )
minuteLabel:setTextColor( 131, 255, 131, 255 )
clock:insert( minuteLabel, true )
minuteLabel.x = 240; minuteLabel.y = 250
local secondLabel = display.newText( "seconds ", 0, 0, “Zapfino”, 40 )
secondLabel:setTextColor( 131, 255, 131, 255 )
clock:insert( secondLabel, true )
secondLabel.x = 230; secondLabel.y = 400
– only get time once
local time = os.date("*t")
local secondText = 0
local function updateTime()
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
if secondText == 0 then
secondText=1
else
secondText=0
end
secondField.text = secondText
if (secondText < 10) then secondField.text = “0” … secondField.text end
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 ) [import]uid: 3867 topic_id: 623 reply_id: 1276[/import]