More on lua tables here:
I finally got time to tinker around a bit this morning (I’ve got 4 kids and a wife who is always working…hard to find time for this lol).
I created the crate table, like you explained above, but it gave me this error…I took screenshots of the error and where it took place in the code. What did I do wrong??? =/
Also, what is the purpose of adding the event listener at the bottom into a for loop?
Hey i changed the image path in the line 51 for testing point it to correct image, my bad.
How did I not catch that? Haha.
I’m able to use the table now, but when I click on the crate I get this error [image attached].
Here’s where the error occured:
function crateTouched(event)
if event.phase == “began” then
self.markX = self.x
self.markY = self.yelseif event.phase == “moved” then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
endreturn true
end– Add event listener
circle:addEventListener(“touch”, circleTouched)for i = 0, 10 do
crateTable[i]:addEventListener(“touch”, crateTouched)
end
It’s on the line that says “self.markX = self.x”
You are mixing the concept of “table” listeners (which don’t mean because you’re objects are in tables, btw) and function listeners. All of your code including how you add the event listener and define the function are done as “function listeners” (my preferred way, btw).
Where you’re mixing the concepts is this part of the code:
self.markX = self.x
self.markY = self.y
There is no “self” with function handlers. Instead event.target represents the object that’s being interacted with. You can do:
event.target.markX = event.target.x
event.target.markY = event.target.y
instead.
Rob
Thanks =D
Now I’m getting "attempting to perform arithmetics on field “markX” (a nil value)?
Does markX need to be defined somewhere in the code?
I’ll show you what line gave the error:
crateTable = {}
for i = 0, 10 do
crateTable[i] = display.newImageRect(“images/crate2.png”, 25, 25)
crateTable[i].anchorX = 0.0
crateTable[i].x = display.contentCenterX + 50
crateTable[i].y = (display.contentCenterY - 100) + 10*i
physics.addBody(crateTable[i], “dynamic”, {density = 1.0, friction = 0.3, bounce= 0.0, isSensor = false})
end
function circleTouched(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(circle)
elseif event.phase == “ended” then
circle:applyLinearImpulse(((event.xStart - event.x) / 4), ((event.yStart - event.y) / 4), circle.x, circle.y)
display.getCurrentStage():setFocus(nil)
end
end
function crateTouched(event)
if event.phase == “began” then
– The error is just below here on markX (nil value)
event.target.markX = event.target.x
event.target.markY = event.target.y
elseif event.phase == “moved” then
local x = (event.x - event.xStart) + event.target.markX
local y = (event.y - event.yStart) + event.target.markY
end
return true
end
– Add event listener
circle:addEventListener(“touch”, circleTouched)
for i = 0, 10 do
crateTable[i]:addEventListener(“touch”, crateTouched)
end
It’s possible to get a “moved” phase without getting “began” phase. You define .markX and .markY in the began phase, which should work most of the time. But it’s always best to initialize these things when you create the object.
Where you do:
crateTable[i].x = display.contentCenterX + 50
crateTable[i].y = (display.contentCenterY - 100) + 10*i
add:
crateTable[i].markX = crateTable[i].x
crateTable[i].markY = crateTable[i].y
to record your starting position.
Rob
Thanks.
No error this time but the blocks won’t do anything when I try to drag them.
Is this due to the object having physics properties?
Hi Nick,
There’s no reason physics should be preventing the blocks from moving in this way… well, technically it could but that would be more advanced, like the blocks were welded/joined to some other static object, etc… and I doubt you’ve done anything like that.
Brent