I have tried something, plz. check the following code
– Analog Clock App
– Developed by Carlos Yanez
– Background
local background = display.newImageRect(“background.png”, 640, 960)
background.x = display.contentWidth/2;
background.y = display.contentHeight/2;
– Clock hands
local hourHand = display.newImage(“hourHand.png”, 152, 185)
local minuteHand = display.newImage(“minuteHand.png”, 152, 158)
local center = display.newImage(“center.png”, 150, 230)
local secondHand = display.newImage(“secondHand.png”, 160, 155)
local myText = display.newText(“Waiting…”,(display.viewableContentWidth / 2), 40, native.systemFont, 30)
myText:setTextColor(0, 0, 0)
hourHand.id = ‘hourHand’
minuteHand.id = ‘minuteHand’
hourHand:setReferencePoint(display.BottomCenterReferencePoint)
minuteHand:setReferencePoint(display.BottomCenterReferencePoint)
secondHand:setReferencePoint(display.BottomCenterReferencePoint)
hourHand.x = display.contentWidth/2;
hourHand.y = display.contentHeight/2;
minuteHand.x = display.contentWidth/2;
minuteHand.y = display.contentHeight/2;
secondHand.x = display.contentWidth/2;
secondHand.y = display.contentHeight/2;
center.x = display.contentWidth/2;
center.y = display.contentHeight/2;
– Set clock hands position at start
local timeTable = os.date("*t")
hourHand.rotation = timeTable.hour * 30 + (timeTable.min * 0.5)
myText.text = “Drag to rotate”
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6
timeTable = nil
– Reposition every second
local function moveHands(e)
local timeTable = os.date("*t")
hourHand.rotation = timeTable.hour * 30 + (timeTable.min * 0.5)
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6
timeTable = nil
end
–timer.performWithDelay(1000, moveHands, 0)
local function rotateObj(event)
local t = event.target
local phase = event.phase
local angle
if (phase == “began”) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif t.isFocus then
if (phase == “moved”) then
angle = math.atan2(event.y - display.contentHeight/2, event.x - display.contentWidth/2); --get angle in radians
angle = angle * 180/math.pi + 90; --convert to degrees , the 90 is to have it point to the mouse
myText.text = angle + 90
t.rotation = angle; --rotate
elseif (phase == “ended”) then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
angle = nil
t = nil
phase = nil
–myText.text = t.id…’ || R: ‘…hourHand.rotation…’ || D: '…math.deg(hourHand.rotation)
end
end
return true
end
hourHand:addEventListener(“touch”, rotateObj) [import]uid: 88119 topic_id: 14969 reply_id: 57277[/import]