I'm new to corona and was trying to do some kind of object pooling on moving platforms. When they exit the screen theyre moved from visibleBlocks to blocks. When I log counts of both my tables the numbers dont match the way they should.. and there are long gaps in my blocks appearing on screen.
My blocks generation:
local color = 'red'
for i = 1, 10 do
local block = display.newRect( 0, 0, 60, 2 )
block.index = i
block.name = 'block'
block.isVisible = false
physics.addBody( block, 'static' )
blocks[i] = block
sceneGroup:insert( block )
block.color = color
color = switchColor(block)
end
code detecting when blocks left the screen
function update()
for k, block in pairs(visibleBlocks) do
block.y = block.y - 1
if block.y < 0 then
removeBlock(block)
end
end
removeblock code
function removeBlock(block)
block.isVisible = false
block.isBodyActive = false
blocks[block.index] = block
visibleBlocks[block.index] = nil
print (' ')
print( 'blocks: ' .. #blocks)
print( 'visible blocks: ' .. #visibleBlocks )
end
addblock which is executed on a timer
function addBlock()
local block = table.remove( blocks )
if block ~= nil then
block.isVisible = true
block.isBodyActive = true
visibleBlocks[block.index] = block
block.x = math.random(
block.contentWidth/2 + 20,
display.contentWidth - block.contentWidth/2 - 20
)
block.y = display.contentHeight
end
end
my console output looks like this:
Oct 23 08:50:16.281: blocks: 0
Oct 23 08:50:16.281: visible blocks: 9
Oct 23 08:50:17.289:
Oct 23 08:50:17.290: blocks: 0
Oct 23 08:50:17.290: visible blocks: 8
Oct 23 08:50:18.329:
Oct 23 08:50:18.329: blocks: 10
Oct 23 08:50:18.329: visible blocks: 7
Oct 23 08:50:19.353:
Oct 23 08:50:19.354: blocks: 9
Oct 23 08:50:19.354: visible blocks: 6
Oct 23 08:50:20.313:
Oct 23 08:50:20.314: blocks: 8
Oct 23 08:50:20.314: visible blocks: 5
Oct 23 08:50:21.339:
Oct 23 08:50:21.340: blocks: 0
Oct 23 08:50:21.340: visible blocks: 10
Oct 23 08:50:22.376:
Oct 23 08:50:22.376: blocks: 7
Oct 23 08:50:22.376: visible blocks: 10
Oct 23 08:50:23.390:
Oct 23 08:50:23.390: blocks: 6
Oct 23 08:50:23.390: visible blocks: 10
Oct 23 08:50:24.392:
Oct 23 08:50:24.393: blocks: 5
Oct 23 08:50:24.393: visible blocks: 10
Oct 23 08:50:25.457:
Oct 23 08:50:25.457: blocks: 4
Oct 23 08:50:25.458: visible blocks: 10
these numbers should always add up to 10 right? something isnt quite right here
#is only defined on tables without holes. - Etan Reisner