How do you make an object snap to angle when rotating to touch? That means for example, instead of a smooth continuous rotation, the object rotates in 45 degree intervals. >> 0°, 45°, 90°, 135°, 180°, 225°, 270°, 315°, 360°
Here’s my current code:
local myRect, start = display.newRect( display.contentCenterX, display.contentCenterY, 100, 300 ) local function rotate(e) if( e.phase == "began" ) then display.getCurrentStage():setFocus( myRect ) local dx = e.x - myRect.x local dy = e.y - myRect.y start = (math.atan2(dy,dx) \* 180 / math.pi) - myRect.rotation elseif( e.phase == "moved" ) then display.getCurrentStage():setFocus( myRect ) local dx = e.x - myRect.x local dy = e.y - myRect.y myRect.rotation = ( (math.atan2(dy,dx) \* 180 / math.pi) - start ) % 360 elseif( e.phase == "ended" ) then display.getCurrentStage():setFocus( nil ) end end myRect:addEventListener("touch", rotate)