I am running into two problems with my drag / drop code.
I am moving puzzle pieces on the screen, and I have a function movePiece(event) that is used to listen to touch event.
local function movePiece(event)
-- @TODO attempt to perform arithmetic on field 'markedX' (a nil value)
if event.phase == "began" then
-- bring piece to front
event.target:toFront()
--lock focus
display.getCurrentStage():setFocus(event.target)
event.target.hasFocus = true;
event.target.markedX = event.target.x -- store x location of object
event.target.markedY = event.target.y -- store y location of object
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + event.target.markedX
local y = (event.y - event.yStart) + event.target.markedY
event.target.x, event.target.y = x, y -- move object based on calculations above
This code works, I do have one problem that crops up every so often that throws errors in the console but does not affect execution of the app.
First issue, I initially tried to create a markedX & markedY and assign event.target.(x/y). But it kept saying markedX was nil. If I changed it to event.target.markedX it works fine. Why can’t I create a local variable and assign event.target.x/y directly to that rather than having to add it to the event object?
2nd question, I occasionally get an error in the console:
attempt to perform arithmetic on field 'markedX' (a nil value)
It seems only to affect the X value, not the Y value, and it only happens once and a while.
[import]uid: 160288 topic_id: 33201 reply_id: 333201[/import]