How could I make this more efficent?

local function draggable(event) local target = event.target if event.phase == "began" then target.markX = target.x -- store x location of object target.markY = target.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + target.markX local y = (event.y - event.yStart) + target.markY target.x, target.y = x, y -- move object based on calculations above end return true end object:addEventListener("touch",draggable)

It loses the target if you move too fast and it interferes with other objects with this event is attached to.

Putting target:toFront() helps makes it interfere less with other object it seems but it still loses focus of the object if you drag too fast

You need to set focus to ensure the event listener keeps receiving the events for that object and you’d be much better off with a table listener.

Read this:

https://docs.coronalabs.com/daily/api/type/StageObject/setFocus.html

This is closer to what you want:

local function ( self, event ) local eventID = event.id if(event.phase == "began") then display.getCurrentStage():setFocus( self ) self.isFocus = true self.x0 = self.x self.y0 = self.y elseif(self.isFocus) then self.x= self.x0 + event.x - event.xStart self.y = self.y0 + event.y - event.yStart if(event.phase == "ended") then display.getCurrentStage():setFocus( nil ) self.isFocus = false end end return true end local dragObj = display.newCircle( 100, 100, 30 ) dragObj.touch = touch dragObj:addEventListener( "touch" )

Thanks.

Putting target:toFront() helps makes it interfere less with other object it seems but it still loses focus of the object if you drag too fast

You need to set focus to ensure the event listener keeps receiving the events for that object and you’d be much better off with a table listener.

Read this:

https://docs.coronalabs.com/daily/api/type/StageObject/setFocus.html

This is closer to what you want:

local function ( self, event ) local eventID = event.id if(event.phase == "began") then display.getCurrentStage():setFocus( self ) self.isFocus = true self.x0 = self.x self.y0 = self.y elseif(self.isFocus) then self.x= self.x0 + event.x - event.xStart self.y = self.y0 + event.y - event.yStart if(event.phase == "ended") then display.getCurrentStage():setFocus( nil ) self.isFocus = false end end return true end local dragObj = display.newCircle( 100, 100, 30 ) dragObj.touch = touch dragObj:addEventListener( "touch" )

Thanks.