Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created June 14, 2013 20:59
Show Gist options
  • Select an option

  • Save figengungor/5785233 to your computer and use it in GitHub Desktop.

Select an option

Save figengungor/5785233 to your computer and use it in GitHub Desktop.
Add focus to drag code so the objects won't interfere with each other while dragging on the screen.
-- create objects
local rect1 = display.newRect( 0, 0, 100, 100 )
local rect2 = display.newRect(300,300, 50,50)
-- touch listener function
local function move(self, event )
if event.phase == "began" then
--set focus on touched object so it won't interfere with other display objects.
display.getCurrentStage():setFocus( self, event.id )
self.isFocus = true
--store the first location of the object
self.x0 = self.x -- store x location of object
self.y0 = self.y -- store y location of object
elseif event.phase == "moved" then
--add the amount of distance that you move the object, to first location of the object
local x = (event.x - event.xStart) + self.x0
local y = (event.y - event.yStart) + self.y0
self.x, self.y = x, y -- move object based on calculations above
elseif event.phase=="ended" or event.phase=="canceled" then
--remove the focus on the object
display.getCurrentStage():setFocus( self, nil )
self.isFocus = false
end
return true
end
rect1.touch=move
rect2.touch=move
rect1:addEventListener( "touch", rect1)
rect2:addEventListener("touch", rect2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment