how to do this properly?

Hi,

need a helping hand…

[code]
clockOfs = display.stageWidth / 2
clockWidth = 100
clockTextPosition = 85
clockBigMarkWidth = 7
clockSmallMarkWidth = 3
clockXtextCorrection = 3
clockYtextCorrection = 9
x0 = clockOfs
y0 = clockOfs - clockWidth
pi = 4*math.atan(1)
local someColors = {255,0,0}
for i=0,60 do
x1 = math.sin(pi-i/60*2*pi) * clockWidth + clockOfs
y1 = math.cos(pi-i/60*2*pi) * clockWidth + clockOfs
local a = display.newLine(x0, y0, x1, y1)
a:setColor( 0, 255, 0, 255 )
a.width = 1
xv = (x1 - clockOfs) / clockWidth
yv = (y1 - clockOfs) / clockWidth
if math.mod(i, 5) == 0 then
xt = xv * clockTextPosition + clockOfs - clockXtextCorrection
yt = yv * clockTextPosition + clockOfs - clockYtextCorrection
value = math.ceil(i / 5)
if value == 0 then
value = 12
end
local textObject = display.newText( value, xt, yt, native.systemFont, 10 )
textObject:setTextColor( 255,255,255 )
xv = xv * (clockWidth - clockBigMarkWidth) + clockOfs
yv = yv * (clockWidth - clockBigMarkWidth) + clockOfs
local b = display.newLine(x1, y1, xv, yv)
b:setColor( 0, 255, 255, 255 )
b.width = 1
else
xv = xv * (clockWidth - clockSmallMarkWidth) + clockOfs
yv = yv * (clockWidth - clockSmallMarkWidth) + clockOfs
local c = display.newLine(x1, y1, xv, yv)
c:setColor( 255, 255, 0, 255 )
c.width = 1
end
x0 = x1
y0 = y1
end
local function updateTime()
time = os.time()
dateString = os.date("%c", time)
local textObject = display.newText( dateString, 80, 300, native.systemFont, 12 )
textObject:setTextColor( 255,255,255 )
dateFields = os.date("*t", time)
hour = dateFields.hour
if hour < 10 then
hour = “0” … hour
end
min = dateFields.min
if min < 10 then
min = “0” … min
end
sec = dateFields.sec
if sec < 10 then
sec = “0” … sec
end

local textObject = display.newText( hour … “:” … min … “:” … sec, 105, 270, native.systemFont, 24 )
textObject:setTextColor( 255,255,255 )

hour = dateFields.hour
if hour > 12 then
hour = hour - 12
end
hour = hour + dateFields.min / 60 + dateFields.sec / 3600
x = math.sin(pi-hour/12*2*pi) * clockWidth / 3 * 2 + clockOfs
y = math.cos(pi-hour/12*2*pi) * clockWidth / 3 * 2 + clockOfs
local h = display.newLine(clockOfs, clockOfs, x, y)
h:setColor( 255, 0, 0, 255 )
h.width = 1

min = dateFields.min + dateFields.sec / 60
x = math.sin(pi-min/60*2*pi) * clockWidth + clockOfs
y = math.cos(pi-min/60*2*pi) * clockWidth + clockOfs
local m = display.newLine(clockOfs, clockOfs, x, y)
m:setColor( 0, 255, 0, 255 )
m.width = 1

x = math.sin(pi-dateFields.sec/60*2*pi) * clockWidth + clockOfs
y = math.cos(pi-dateFields.sec/60*2*pi) * clockWidth + clockOfs
local s = display.newLine(clockOfs, clockOfs, x, y)
s:setColor( 0, 0, 255, 255 )
s.width = 1

end

local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

[/code] [import]uid: 6553 topic_id: 1184 reply_id: 301184[/import]

Since no one else responded I thought I would leave my comments. I’m just learning the Corona SDK so here is my take on your code.

Your code creates a clock face in the main code. It uses a 1 second timer event to update the date/time fields and the Hour, Minute, and Seconds hands. The code creates all these objects each time within the timer event. The objects are created each time and never reused or removed which is why the display fills up with the newLine objects.

I believe the proper way to handle this is using display groups. You create a clock group and add all the objects to the group. This allows you to remove objects from the group and do other things like hide or move the group of objects.

I modified your code by creating a clock_Grp to hold the Hour/Minute/Seconds hand objects. I also created date and time objects outside of the function so the field can be updated inside the function without generating a new object every time. Inside the timer event I remove the old “hand” objects and create a new one with the new x/y values. I played around with the display.rotate method but couldn’t get it to rotate the line correctly every time.

clockOfs = display.stageWidth / 2   
clockWidth = 100  
clockTextPosition = 85  
clockBigMarkWidth = 7  
clockSmallMarkWidth = 3  
clockXtextCorrection = 3  
clockYtextCorrection = 9  
x0 = clockOfs  
y0 = clockOfs - clockWidth  
pi = 4\*math.atan(1)  
  
-- \*\*fogview: Add the Hours, Minutes, and Seconds to a object group  
-- so they can be removed and updated as needed.  
local h -- Used to store Hours hand object (newLine) -- \*\*fogview  
local m -- Used to store Minutes hand object (newLine) -- \*\*fogview  
local s -- Used to store Seconds hand object (newLine) -- \*\*fogview  
clock\_Grp = display.newGroup() -- create a Clock Group -- \*\*fogview  
  
local someColors = {255,0,0}  
   
for i=0,60 do  
 x1 = math.sin(pi-i/60\*2\*pi) \* clockWidth + clockOfs  
 y1 = math.cos(pi-i/60\*2\*pi) \* clockWidth + clockOfs  
 local a = display.newLine(x0, y0, x1, y1)  
 a:setColor( 0, 255, 0, 255 )  
 a.width = 1  
 xv = (x1 - clockOfs) / clockWidth  
 yv = (y1 - clockOfs) / clockWidth  
 if math.mod(i, 5) == 0 then  
 xt = xv \* clockTextPosition + clockOfs - clockXtextCorrection  
 yt = yv \* clockTextPosition + clockOfs - clockYtextCorrection  
 value = math.ceil(i / 5)  
 if value == 0 then  
 value = 12  
 end  
 local textObject = display.newText( value, xt, yt, native.systemFont, 10 )  
 textObject:setTextColor( 255,255,255 )  
 xv = xv \* (clockWidth - clockBigMarkWidth) + clockOfs  
 yv = yv \* (clockWidth - clockBigMarkWidth) + clockOfs  
 local b = display.newLine(x1, y1, xv, yv)  
 b:setColor( 0, 255, 255, 255 )  
 b.width = 1  
 else  
 xv = xv \* (clockWidth - clockSmallMarkWidth) + clockOfs  
 yv = yv \* (clockWidth - clockSmallMarkWidth) + clockOfs  
 local c = display.newLine(x1, y1, xv, yv)  
 c:setColor( 255, 255, 0, 255 )  
 c.width = 1  
 end  
 x0 = x1  
 y0 = y1  
end  
  
-- \*\*fogview: Create the Time and Date objects outside the function so they can be updated  
-- without creating new objects each time.  
local timeObject = display.newText( "hh" .. ":" .. "mm" .. ":" .. "ss", 105, 270, native.systemFont, 24 )  
timeObject:setTextColor( 255,255,255 )  
  
local dateObject = display.newText( "Mon June 1 00:00:00 2010", 80, 300, native.systemFont, 12 )  
dateObject:setTextColor( 255,255,255 )  
-- \*\*fogview  
  
local function updateTime()   
 time = os.time()  
 dateString = os.date("%c", time)  
 dateObject.text = dateString -- \*\*fogview  
 -- textObject:setTextColor( 255,255,255 ) -- \*\*fogview  
 dateFields = os.date("\*t", time)  
 hour = dateFields.hour  
 if hour \< 10 then  
 hour = "0" .. hour  
 end  
 min = dateFields.min  
 if min \< 10 then  
 min = "0" .. min  
 end  
 sec = dateFields.sec  
 if sec \< 10 then  
 sec = "0" .. sec  
 end  
  
 -- \*\*fogview: update the "time" field here  
 timeObject.text = hour .. ":" .. min .. ":" .. sec -- \*\*fogview  
-- textObject:setTextColor( 255,255,255 ) -- \*\*fogview  
   
 hour = dateFields.hour  
 if hour \> 12 then  
 hour = hour - 12  
 end  
 hour = hour + dateFields.min / 60 + dateFields.sec / 3600  
 x = math.sin(pi-hour/12\*2\*pi) \* clockWidth / 3 \* 2 + clockOfs  
 y = math.cos(pi-hour/12\*2\*pi) \* clockWidth / 3 \* 2 + clockOfs  
  
 -- Hours: Account for the first time through the function  
 if h then -- \*\*fogview  
 -- Need to remove the Seconds hand object  
 clock\_Grp:remove(h) -- \*\*fogview  
 end   
 h = display.newLine(clockOfs, clockOfs, x, y)  
 h:setColor( 255, 0, 0, 255 )  
 h.width = 5  
 clock\_Grp:insert(h) -- \*\*fogview: add Hour hand to group  
  
 min = dateFields.min + dateFields.sec / 60  
 x = math.sin(pi-min/60\*2\*pi) \* clockWidth + clockOfs  
 y = math.cos(pi-min/60\*2\*pi) \* clockWidth + clockOfs  
  
 -- Minutes: Account for the first time through the function  
 if m then -- \*\*fogview  
 -- Need to remove the Seconds hand object  
 clock\_Grp:remove(m) -- \*\*fogview  
 end -- \*\*fogview   
 m = display.newLine(clockOfs, clockOfs, x, y)  
 m:setColor( 0, 255, 0, 255 )  
 m.width = 4  
 clock\_Grp:insert(m) -- \*\*fogview: add Minute hand to group  
  
 x = math.sin(pi-dateFields.sec/60\*2\*pi) \* clockWidth + clockOfs  
 y = math.cos(pi-dateFields.sec/60\*2\*pi) \* clockWidth + clockOfs  
  
 -- Seconds: Account for the first time through the function  
 if s then -- \*\*fogview  
 -- Need to remove the Seconds hand object  
 clock\_Grp:remove(s) -- \*\*fogview  
 end -- \*\*fogview  
 s = display.newLine(clockOfs, clockOfs, x, y)  
 s:setColor( 255, 255, 255, 255 ) -- \*\*fogview  
 s.width = 1  
 clock\_Grp:insert(s) -- \*\*fogview: add Seconds hand to group  
   
end  
   
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )  

BTW, because of a bug in the timer.performWithDelay() method, the clock will stop after 13 minutes (bug #139). The only way I know to keep a counter going (longer than 13 minutes) is to use the enterFrame event and count your seconds from that (using 30 FPS).

I hope this helps.

Tom [import]uid: 6119 topic_id: 1184 reply_id: 3192[/import]

THX :slight_smile: [import]uid: 6553 topic_id: 1184 reply_id: 3222[/import]