I’m sure there are many ways to spawn objects into a table, but this is the scheme I use and it’s fairly easy to follow.
I spawn all similar objects into the same table so when I want to reload or respawn the screen I can clear the display objects from memory with display.remove and nil the table holding the objects easily.
The label is added to the indexed crates table and I only use 1 Runtime listener and iterate through the crates table in it to keep the labels on their parent objects as they bounce around, it also removes any crates that go off the screen
I always spawn objects with an ID of some sort so I can reference them later or remove them, whatever, it comes in handy especially if a touch listener is added. I stop the last crate on the floor and print out its ID.
I don’t mean to confuse you, but here is my version of what I think you are trying to accomplish.
This code can be run, but I use graphics 1.0 so you may have to change the colors. It’s setup for 320x480 portrait view also, but you can change the x,y values to work in landscape.
[lua]
local physics = require(“physics”)
local CratesGroup = display.newGroup()
local crates = {}
local count = 0
local Base
local j
local i
physics.start()
local function removeCrate(event)
local k
for k = #crates, 1, -1 do
crates[k].label.x = crates[k].x
crates[k].label.y = crates[k].y
if #crates == 1 then
print(“Only 1 crate left!!!”)
print(“crates[k].ID == “…crates[k].ID…””)
crates[k]:setLinearVelocity (0, 0)
physics.removeBody( crates[k] )
Runtime:removeEventListener( “enterFrame”, removeCrate )
return
end
if (crates[k].x < -40 or crates[k].x > display.actualContentWidth + 75) then
print(“crates[”…k…"].ID is OFF Screen!")
display.remove(crates[k])
display.remove(crates[k].label)
table.remove(crates, k)
--table.remove(crates, k)
--remove the crate here I guess or do what you want
end
end
end
local function createCrate(i,j)
print(“count == “…count…””)
print(“createCrate/ i == “…i…” j == “…j…””)
– crates[count] = display.newImage( “crate.png”, 300 + (i*90), -5000 - (j*120) )
crates[count] = display.newCircle((i*60) - 30, (j*45), 20 )
physics.addBody(crates[count], {density = 0.01, friction = .1, bounce = 1})
crates[count].ID = count --give it an ID so you can reference it later so you can identify it if needed
CratesGroup:insert(crates[count])
--crates[count]:setStrokeColor(255, 127, 36 )
crates[count]:setFillColor(255, 255,255)
--add label
local _label = “”…count…""
crates[count].label = display.newText( _label, crates[count].x, crates[count].y, native.systemFont, 12 )
CratesGroup:insert(crates[count].label)
crates[count].label:setFillColor(0, 0,0)
end
local function spawnCrates()
for i = 1, 5 do
for j = 1, 5 do
count = count + 1
createCrate(i,j)
end
end
--add the Runtime listener once as it iterates over all the crates
Runtime:addEventListener( “enterFrame”, removeCrate )
Base = display.newRect(0, 460, 320, 15 )
– physics.addBody(Base, {density = 0.01, friction = 1, bounce = 0})
physics.addBody(Base, “static”)
end
spawnCrates()
[/lua]
hope this helps,
Nail