Removing items from a table within an enterFrame listener that iterating through the table

I’m currently trying to replicate “Swipe Brick Breaker” a clone of one of the games made by 111% games.

https://www.youtube.com/watch?v=NrjuN6Vx7KM

<------ (this is what I’m trying to make)

I currently have 3 balls moving on screen and I would like them to stop moving and get deleted when they reach the bottem of the screen. The balls are being iterated through a ballTable with the enterFrame listener so that they can all move. I thought removing the ball being iterated with removeSelf() and then setting the ball to be = nil would allow me to do this (as I have an if ball ~= nil statement which should bypass the function if ball = nil). Here’s what I have: 

[spoiler]

function updatenewbullet() for key,ball in pairs(ballTable) do if ball ~= nil then -- don't do rest of function is ball is nil? NOT WORKING -- Movement ball.x = ball.x + ball.velocityX ball.y = ball.y + ball.velocityY -- If ball hits the ceiling or left or right wall, bounce off of it if ball.x \< 0 or ball.x + ball.width \> display.contentWidth then ball.velocityX = -ball.velocityX end -- Bounce off top wall if ball.y \< -30 then ball.velocityY = -ball.velocityY end -- stop if ball reaches bottem wall if ball.y \> display.contentHeight + 20 then ball.velocityY = 0 ball.velocityX = 0 ball:removeSelf() -- remove ball ball = nil -- set ball to nil end end end end &nbsp;

As shown I have set my ball to nil when it’s reached the bottem of the display but function continues despite the “if ball ~= nil then” and the ball being nil. 

[/spoiler]

 -- stop if ball reaches bottem wall if ball.y \> display.contentHeight + 20 then ballTable[key] = nil -- CLEAR BALL FROM TABLE display.remove( ball ) -- remove ball end

This WILL NOT WORK if the table is numerically indexed.  

I suggest NOT using a numerically indexed table if you can avoid it.

Check back in if your table is numerically indexed.

Ok it works! When you suggest not using a numerically indexed table, is this for general or just in my case of adding my balls in a table?

Both styles of table are useful, but numerically indexed tables suck for removal of entries during iteration.

Example 1 - Fine & Dandy

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[tmp] = tmp end -- Randomly remove entries while iterating -- for k, v in pairs( objs ) do if( math.random(1,2) == 2 ) then objs[k] = nil display.remove(v) end end

Example 2 - Creates Holes In Table

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for k, v in pairs( objs ) do if( math.random(1,2) == 2 ) then objs[k] = nil display.remove(v) end end

Example 3 - Breaks Iterator (Probably Crashes)

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for i = 1, #objs do if( math.random(1,2) == 2 ) then display.remove(objs[i]) table.remove( objs, i ) end end

Example 4 - Fixes Last Example To Make It Safe?

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for i = #objs, 1, -1 do if( math.random(1,2) == 2 ) then display.remove(objs[i]) table.remove( objs, i ) end end
 -- stop if ball reaches bottem wall if ball.y \> display.contentHeight + 20 then ballTable[key] = nil -- CLEAR BALL FROM TABLE display.remove( ball ) -- remove ball end

This WILL NOT WORK if the table is numerically indexed.  

I suggest NOT using a numerically indexed table if you can avoid it.

Check back in if your table is numerically indexed.

Ok it works! When you suggest not using a numerically indexed table, is this for general or just in my case of adding my balls in a table?

Both styles of table are useful, but numerically indexed tables suck for removal of entries during iteration.

Example 1 - Fine & Dandy

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[tmp] = tmp end -- Randomly remove entries while iterating -- for k, v in pairs( objs ) do if( math.random(1,2) == 2 ) then objs[k] = nil display.remove(v) end end

Example 2 - Creates Holes In Table

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for k, v in pairs( objs ) do if( math.random(1,2) == 2 ) then objs[k] = nil display.remove(v) end end

Example 3 - Breaks Iterator (Probably Crashes)

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for i = 1, #objs do if( math.random(1,2) == 2 ) then display.remove(objs[i]) table.remove( objs, i ) end end

Example 4 - Fixes Last Example To Make It Safe?

-- Create table with entries indexed by object handle -- local objs = {} for i = 1, 100 do local tmp = display.newCircle( 10, 10, 10 ) objs[i] = tmp end -- Randomly remove entries while iterating -- for i = #objs, 1, -1 do if( math.random(1,2) == 2 ) then display.remove(objs[i]) table.remove( objs, i ) end end