The phrase you’re looking for is drag and drop.
SSK2 comes with a helper that you can use OR steal the code from:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc/#addsmartdrag
Code is here (lines 407 …465):
https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/misc.lua
If you grab the code, you’ll need to remove some parts if you’re not using SSK:
This should do it (may still have issues as I’m not going to test it, but I removed the parts that made it dependent upon SSK):
local function addSmartDrag( obj, params ) params = params or {} obj.touch = function( self, event ) local phase = event.phase local id = event.id if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self, id ) self.x0 = self.x self.y0 = self.y if( params.toFront ) then self:toFront() end if( self.onDragged ) then self:onDragged( { obj = self, phase = event.phase, x = event.x, y = event.y, dx = 0, dy = 0, time = getTimer(), target = self } ) end target = self } ) if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end elseif( self.isFocus ) then local dx = event.x - event.xStart local dy = event.y - event.yStart self.x = self.x0 + dx self.y = self.y0 + dy event.dx = dx event.dy = dy if( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self, nil ) if( self.onDragged ) then self:onDragged( { obj = self, phase = event.phase, x = event.x, y = event.y, dx = dx, dy = dy, time = getTimer(), target = self } ) end if( self.onDropped ) then self:onDropped( { obj = self, phase = event.phase, x = event.x, y = event.y, dx = dx, dy = dy, time = getTimer(), target = self } ) end getTimer(), target = self } ) getTimer(), target = self } ) else if( self.onDragged ) then self:onDragged( { obj = self, phase = event.phase, x = event.x, y = event.y, dx = dx, dy = dy, time = getTimer(), target = self } ) end getTimer(), target = self } ) end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) end end if( params.listener ) then return params.listener( self, event ) or fnn(params.retval,false) else return fnn(params.retval,false) end end; obj:addEventListener("touch") end
You could use it like this:
-- Define addSmartDrag above this line local obj = display.newCirlce( 100, 100, 10 ) function obj.onDropped( self ) self:setFillColor(1,0,0) end addSmartDrag( obj )
Now, when you drag the circle it will move and when you release/drop it, it will turn red.
That should be plenty of a starting point to do what you need.
Finally, there is also a physics dragger in SSK (see same docs page)