Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joehinkle11/1ee847f3076cab4374b7d18e893340f7 to your computer and use it in GitHub Desktop.
Save joehinkle11/1ee847f3076cab4374b7d18e893340f7 to your computer and use it in GitHub Desktop.
--pan
local lastX = 0
local lastY = 0
local touchNum = 0
local touch1 = false
local touch2 = false
local touch1X = 0
local touch1Y = 0
local touch2X = 0
local touch2Y = 0
local zoomStart = 0
local distStart = 0
local dragOkay = false
function onObjectTouch( event )
--zoom
if touchNum > 1 and touchNum < 3 then
gameGroup.xScale = math.max(1,math.min(10,zoomStart*(math.sqrt((touch2X-touch1X)*(touch2X-touch1X) + (touch2Y-touch1Y)*(touch2Y-touch1Y)))/distStart))
gameGroup.yScale = gameGroup.xScale
end
if event.phase == "began" then
lastX = event.x
lastY = event.y
--add touch
touchNum = touchNum + 1
--add touch id
if touch1 == false then
touch1 = event.id
touch1X = event.x
touch1Y = event.y
elseif touch2 == false then
touch2 = event.id
touch2X = event.x
touch2Y = event.y
--remember start of zoom
zoomStart = gameGroup.xScale
distStart = math.sqrt((touch2X-touch1X)*(touch2X-touch1X) + (touch2Y-touch1Y)*(touch2Y-touch1Y))
end
elseif event.phase == "moved" then
--this is the first touch
if touch1 == event.id then
touch1X = event.x
touch1Y = event.y
end
--this is the second touch
if touch2 == event.id then
touch2X = event.x
touch2Y = event.y
end
--zoom
if touchNum > 1 and touchNum < 3 then
dragOkay = false
gameGroup.xScale = math.max(1,math.min(10,zoomStart*(math.sqrt((touch2X-touch1X)*(touch2X-touch1X) + (touch2Y-touch1Y)*(touch2Y-touch1Y)))/distStart))
gameGroup.yScale = gameGroup.xScale
elseif touchNum > 1 then
dragOkay = false
end
else
--remove touch
touchNum = touchNum - 1
--okay to drag
if touchNum == 0 then
dragOkay = true
end
--remove touch id
if touch1 == event.id then
touch1 = false
elseif touch2 == event.id then
touch2 = false
end
end
if touchNum == 1 and dragOkay == true then
gameGroup.x = math.min( gameGroup.width,math.max(-gameGroup.width, gameGroup.x + event.x - lastX))
lastX = event.x
gameGroup.y = math.min( gameGroup.height,math.max(-gameGroup.height,gameGroup.y + event.y - lastY))
lastY = event.y
end
--fix bg
background.x = gameGroup.x*(1-gameGroup.xScale)*.1 + screenCX
background.y = gameGroup.y*(1-gameGroup.xScale)*.1 + screenCY
background.xScale = gameGroup.xScale
background.yScale = gameGroup.yScale
return true
end
Runtime:addEventListener( "touch", onObjectTouch )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment