Skip to content

Instantly share code, notes, and snippets.

@Mr-Coxall
Created April 16, 2018 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr-Coxall/63d5cc75dabc9351882ee764b24ad125 to your computer and use it in GitHub Desktop.
Save Mr-Coxall/63d5cc75dabc9351882ee764b24ad125 to your computer and use it in GitHub Desktop.
-- Comprehensive touch code
local theBall = display.newImage( "./assets/sprites/ball.png" )
theBall.x = display.contentCenterX
theBall.y = display.contentCenterY
theBall.id = "ball object"
function theBall:touch( event )
if ( event.phase == "began" ) then
print( "Touch event began on: " .. self.id )
-- Set touch focus
display.getCurrentStage():setFocus( self )
self.isFocus = true
elseif ( self.isFocus ) then
if ( event.phase == "moved" ) then
print( "Moved phase of touch event detected." )
elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
-- Reset touch focus
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
print( "Touch event ended on: " .. self.id )
end
end
return true
end
theBall:addEventListener( "touch", theBall )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment