This is not really an error, but it does not work the way I want it to work. What I have done is a gave every crate an id that matches a label I placed on them. The problem is I want it to print if a crate has gone off the screen, I have it on an enterFrame listener, but it only prints it for on crate. Why is this happening? This is my code, also, how could I get it to work for the crates that immediately spawn. Right now, I am only making it for crates the user spawns:
for i = 1, 5 do --immediately spawned 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 function finalize( self ) Runtime:removeEventListener( "enterFrame", self.label ) end function finalizeSpawned(self) Runtime:removeEventListener("enterFrame", self.newlabels) end function createCrate(x,y)--user spawns spawnedCrate = display.newImageRect("crate.png", 90, 90) spawnedCrate.x = x spawnedCrate.y = y physics.addBody(spawnedCrate, "dynamic", {density = 0.1, friction = 1, bounce = 0}) spawnedCrateCount = spawnedCrateCount + 1 spawnedCrate.id = #crates + spawnedCrateCount print(spawnedCrate.id) newlabels = display.newText(crate.parent, #crates + spawnedCrateCount, 0, 0) newlabels.crate = spawnedCrate newlabels.enterFrame = enterFrame Runtime:addEventListener("enterFrame", newlabels) end function lostCrates() if spawnedCrate == nil then --nothing else if (spawnedCrate.x \< 0 or spawnedCrate.x \> display.actualContentWidth) then print("Crate Number "..spawnedCrate.id.. " was lost") spawnedCrate = nil end end end Runtime:addEventListener("enterFrame", lostCrates) function createLabels() for x = 1, #crates do crate = crates[x] crate.id = x print(crate.id) -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, x, 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 createLabels()
EDIT: Ignore this for now, and please focus on the question below, I will ask this once the comment under this is solved.