Skip to content

Instantly share code, notes, and snippets.

@yurihan
Last active December 19, 2015 04:58
Show Gist options
  • Save yurihan/5900654 to your computer and use it in GitHub Desktop.
Save yurihan/5900654 to your computer and use it in GitHub Desktop.
simple dpad code.
local DPad = {}
function DPad.create(radius)
local dpad = display.newGroup()
dpad.position = {x=0,y=0}
dpad.angle = 0
dpad.distance = 0
local outterCircle = display.newCircle(dpad, 0, 0, radius )
outterCircle:setFillColor(128,128,128)
outterCircle.alpha = 0.5
local innerCircle = display.newCircle(dpad, 0, 0, radius/3 )
innerCircle:setFillColor(128, 128, 128)
innerCircle.alpha = 0.5
outterCircle:addEventListener('touch',function(event)
if event.phase == 'began' or event.phase == "moved" then
display.getCurrentStage():setFocus( event.target, event.id )
local x, y = event.target:localToContent( 0, 0 )
x, y = event.x-x, event.y-y
-- 내부원이 외부로 나가지 못하게.
local dist = math.sqrt(x*x+y*y)
if dist > radius - radius/3 then
local ix= math.sin(math.atan2(x, y)) * (radius - radius/3)
local iy= math.cos(math.atan2(x, y)) * (radius - radius/3)
innerCircle.x, innerCircle.y = ix, iy
dpad.position.x, dpad.position.y = ix/(radius-radius/3), iy/(radius-radius/3)
else
innerCircle.x, innerCircle.y = x, y
dpad.position.x, dpad.position.y = x/(radius-radius/3), y/(radius-radius/3)
end
-- print(dpad.position.x .. ' ' .. dpad.position.y)
elseif event.phase == "ended" or event.phase == "cancelled" then
dpad.position.x, dpad.position.y = 0, 0
innerCircle.x, innerCircle.y = 0, 0
display.getCurrentStage():setFocus( event.target, nil )
end
end)
return dpad
end
return DPad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment