Help With Removing And Respawning Objects

Hi,

 

what I am trying to do is to have objects removed as when they pass the end of the screen and after they’ve been removed they will eventually be recreated at the top of the screen. This is where I’m stuck.

 

The code used to randomly spawn and create my objects (bubbles):
 

 for i = 1, 15 do bubble[i] = display.newImage("img/bubble.png") physics.addBody( bubble[i], "static", {isSensor = true, radius = 26} ) bubble[i].x = math.random(1,6)\*100 bubble[i].y = math.random(1,10)\*-100 group:insert(bubble[i]) end for j = #bubble, 1 do if bubble[j].y \>= display.contentHeight then display.remove(bubble[j]) end end local moveBubbles = function( event ) for i = 1, #bubble do bubble[i].y = bubble[i].y + 20 end end local gameLoop = function ( event ) moveBubbles() end Runtime:addEventListener( "enterFrame", gameLoop)

 

Is the idea of removing and recreating items ineffecient, should I instead ‘load’ all of my objects and then just respawn them at a new location above the screen?The reason to why I want to remove & recreate new objects is because eventually I would like to recreate them in specific patterns created by unique pattern functions, but first I would need the recreation of the bubbles to work.

 

Glad if anyone could take their time to help, thanks in advance.

Maybe try this:

[lua]

    for j = #bubble, 1, -1 do
        if bubble[j] and bubble[j].y >= display.contentHeight then
            display.remove(bubble[j])

            bubble[j] = nil
        end
    end

[/lua]

Wow, I always forget about setting removed objects to nil and then I keep asking myself about the nil errors, time to set up a “NIL” note above my monitor haha.  Thank you Rob!

 

Here’s the reworked version using separate functions to control future actions better:

local createBubbles = function ( event ) for i = 1, 100 do if bubble[i] == nil then bubble[i] = display.newImage("img/bubble.png") physics.addBody( bubble[i], "static", {isSensor = true, radius = 26} ) bubble[i].x = math.random(1,6)\*100 bubble[i].y = math.random(1,10)\*-100 bubble[i].name = "x" group:insert(bubble[i]) end end end local removeBubbles = function ( event ) for j = #bubble, 1, -1 do if bubble[j] and bubble[j].y \>= display.contentHeight then display.remove(bubble[j]) bubble[j] = nil end end end local moveBubbles = function( event ) for i = 1, #bubble do if bubble[i] ~= nil then bubble[i].y = bubble[i].y + 20 end end end local gameLoop = function ( event ) createBubbles() removeBubbles() moveBubbles() end Runtime:addEventListener( "enterFrame", gameLoop)

Maybe try this:

[lua]

    for j = #bubble, 1, -1 do
        if bubble[j] and bubble[j].y >= display.contentHeight then
            display.remove(bubble[j])

            bubble[j] = nil
        end
    end

[/lua]

Wow, I always forget about setting removed objects to nil and then I keep asking myself about the nil errors, time to set up a “NIL” note above my monitor haha.  Thank you Rob!

 

Here’s the reworked version using separate functions to control future actions better:

local createBubbles = function ( event ) for i = 1, 100 do if bubble[i] == nil then bubble[i] = display.newImage("img/bubble.png") physics.addBody( bubble[i], "static", {isSensor = true, radius = 26} ) bubble[i].x = math.random(1,6)\*100 bubble[i].y = math.random(1,10)\*-100 bubble[i].name = "x" group:insert(bubble[i]) end end end local removeBubbles = function ( event ) for j = #bubble, 1, -1 do if bubble[j] and bubble[j].y \>= display.contentHeight then display.remove(bubble[j]) bubble[j] = nil end end end local moveBubbles = function( event ) for i = 1, #bubble do if bubble[i] ~= nil then bubble[i].y = bubble[i].y + 20 end end end local gameLoop = function ( event ) createBubbles() removeBubbles() moveBubbles() end Runtime:addEventListener( "enterFrame", gameLoop)