Hi,
I currently have a bot object that can be dragged across the screen. When the object is touched and dragged, its drag position is relative to where the object is tapped. However, if the user touches the screen (not on object) and drags onto the object, an error causes the game to crash (regarding x0 and y0). Is there a way to make it so that I can drag an object relative to its touch position (so it does not snap to the center of the touch), while ensuring that if the user touches and drags onto the object nil value errors do not occur.
Here’s my code:
local function botTouch(self, event) local p = event.phase local t = event.target if p == "began" then -- print("bot:touch - began") local parent = t.parent parent:insert(t) display.getCurrentStage():setFocus(t) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif p == "moved" then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif p == "cancelled" or p == "ended" then -- print("bot:touch - ended") display.getCurrentStage():setFocus(nil) t.isFocus = false end return true end function botNew(x, y) print("botNew()") local o = display.newRect(x, y, 32, 32) o.type = "bot" o.touch = botTouch o:setFillColor(0) physics.addBody(o, { density=1, friction=0, bounce=0}) o.isSensor = true o:addEventListener("touch") return o end local b1 = botNew(100,100)
Thanks!