Here’s the full code I tested out.
[lua]-- compute angle between two sets of points
local function angleBetweenPoints ( srcObjx, srcObjy, dstObjx, dstObjy )
– make sure we never return -1.#IND if we try to find angle between identical points
if ( srcObjx == dstObjx and srcObjy == srcObjy ) then return 0 end
– Original angleBetween
local xDist = dstObjx - srcObjx ; local yDist = dstObjy - srcObjy
local angleBetween = math.deg( math.atan( yDist / xDist ) )
if ( srcObjx < dstObjx ) then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end
– These tend to get flipped around, this is a quick fix
if ( angleBetween == 0 ) then angleBetween = -180
elseif ( angleBetween == -180 ) then angleBetween = 0
end
return angleBetween
end
– display the circle
local circle = display.newImageRect( “circle.png”, 256, 256 )
circle.x = display.contentWidth / 2
circle.y = display.contentHeight / 2
– what to do when circle is touched
local function accessCircle( event )
local t = event.target
– is the circle being touched?
if event.phase == “began” then
– keep touch focus on the circle
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
– store initial angle
t.angle0 = t.rotation
elseif t.isFocus then
– is finger moving around circle?
if event.phase == “moved” then
t.x1 = event.x
t.y1 = event.y
local newAngle = math.ceil( angleBetweenPoints ( t.x, t.y, t.x1, t.y1 ))
– rotate circle based on original angle (use t.rotation = newAngle for direct angle)
t.rotation = t.angle0 + newAngle
– did finger let go of circle?
elseif event.phase == “ended” or event.phase == “cancelled” then
– release focus for other objects
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end
– let circle object listen for touch events
circle:addEventListener( “touch”, accessCircle )[/lua]
Create a circular image that you can check its orientation (put something in the circle) and call it circle.png and put it in the same folder as the code above. When you touch the circle, it will rotate based on how you move your finger around it. [import]uid: 6084 topic_id: 11708 reply_id: 42788[/import]