Right now, I am trying to make this function work:
function crateLost() if (crate.x \< 0 or crate.x \> display.actualContentWidth + 75) then --line 137 crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) print("Crate number "..crate.id.. "was lost") crate = nil end end
But the printing does not work, it just gives me an error the says: Attempt to index upvalue ‘crate’ (a nil value), and it points to line 137, which is labeled in the code.
I don’t know why this is happening. this is the code that creates my crates:
for i = 1, 5 do for j = 1, 5 do crates[count] = display.newImage( "crate.png", 300 + (i\*90), -5000 - (j\*120) ) physics.addBody(crates[count], {density = 0.1, friction = 1, bounce = 0}) count = count+1 end end
And this is the code that creates labels for those crates:
function createLabels() for a = 1, #crates do crate = crates[a] crate.id = a --print(crate.id) -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, a, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end
This is happening because I niled out the crate, but it only prints that one crate was lost and does this forever. It repeats it forever because it is on an enterFrame event listener. ( Runtime:addEventListener(“enterFrame”) ) I understand why this happens, but I don’t know how to fix it.