How to remove automatically spawned physics bodies?

Okay, I am looking for a command that will delete all of these physics bodies that have been automatically spawned, and I want them to be deleted when the scene changes or exits. I have searched on the internet for this type of thing, but it seems nothing like this exists (either that or I’m blind :P). I’m new at coding, so try to keep your answers as simple as possible.

The code:

[lua]

–locals

function onCobbleTouch(self, event)

    if(event.phase == “began”) then

    timer.performWithDelay(1, function() self:removeSelf() end )

 end

return true

end

local infinite = 1000000000000000000000000000000000000000000000000000

–spawning command

   function spawnCobble( event )

        local cobbleBlock = display.newImageRect( “cobblestone.png”, 50, 50 )

        cobbleBlock.x = math.random( 320 )

        cobbleBlock.y = -100

        cobbleBlock.rotation = math.random( 360 )

        physics.addBody( cobbleBlock, { density=13, friction= 0.75, bounce= 0.1 } )

        cobbleBlock.touch = onCobbleTouch

        cobbleBlock:addEventListener(“touch”, cobbleBlock)

        group:insert( cobbleBlock )

        return cobbleBlock

    end

        

    blockTimer = timer.performWithDelay( 300, spawnCobble, infinite)

[/lua]

I want all of these physics bodies (cobbleBlock) that have been automatically spawned (in the timer.performWithDelay command) to be deleted when the scene changes or exits. Any help appreciated.

P.S. This is my first post on this forum so if I stuffed anything up please let me know.

One suggestion I have would be to make an empty table:

local blocks = {}

Then at the end of your spawnCobble function put

blocks[#blocks+1] = cobbleBlock

This will add all your blocks into a table. When removing all the blocks use a for loop.

for i=1, #blocks do blocks[i]:removeSelf() end

hope this helps you out a bit!

ALSO:

blockTimer = timer.performWithDelay( 300, spawnCobble, infinite)

should just be:

blockTimer = timer.performWithDelay( 300, spawnCobble, 0)

No need to make an infinite variable, 0 makes it loop forever :wink:

 Thanks for your help, RedPistonEB, you have no idea how long I have been waiting for somebody to reply to this topic. Anyway, it seems you only partially fixed the problem. When I exit the scene the first time it removes the physics bodies like intended, but when I load that scene back up and then exit it again, it comes up with an error message that says it doesn’t know what ‘removeSelf’ means and claims that it is a nil value.

Here is the error message:

[lua]

…documents/corona projects/crates/cobblegame.lua:140: attempt to call method 'removeSelf (a nil value)

stack traceback:

             [C]: in function ‘removeSelf’

             …documents/corona projects/crates/cobblegame.lua:140: in function

             <…documents/corona projects/crates/cobblegame.lua:132>

             ?: in function ‘dispatchEvent’

             ?: in function ‘?’

             ?: in function ‘gotoScene’

             …documents/corona projects/crates/cobblegame.lua:89: in function ‘_onRelease’

             ?: in function ‘?’

             ?: in function <?:1091>

             ?: in function <?:218>

[/lua]

Again, thanks for helping me. I did replace the ‘infinite’ with zero, thanks for telling me about that (i’m still a noob at programming).

EDIT: I fixed it, I had to add a ‘nil’ command after removeself, like this:

[lua]

    for i=1, #blocks do

    blocks[i]:removeSelf()

    blocks[i] = nil

    end

[/lua]

One suggestion I have would be to make an empty table:

local blocks = {}

Then at the end of your spawnCobble function put

blocks[#blocks+1] = cobbleBlock

This will add all your blocks into a table. When removing all the blocks use a for loop.

for i=1, #blocks do blocks[i]:removeSelf() end

hope this helps you out a bit!

ALSO:

blockTimer = timer.performWithDelay( 300, spawnCobble, infinite)

should just be:

blockTimer = timer.performWithDelay( 300, spawnCobble, 0)

No need to make an infinite variable, 0 makes it loop forever :wink:

 Thanks for your help, RedPistonEB, you have no idea how long I have been waiting for somebody to reply to this topic. Anyway, it seems you only partially fixed the problem. When I exit the scene the first time it removes the physics bodies like intended, but when I load that scene back up and then exit it again, it comes up with an error message that says it doesn’t know what ‘removeSelf’ means and claims that it is a nil value.

Here is the error message:

[lua]

…documents/corona projects/crates/cobblegame.lua:140: attempt to call method 'removeSelf (a nil value)

stack traceback:

             [C]: in function ‘removeSelf’

             …documents/corona projects/crates/cobblegame.lua:140: in function

             <…documents/corona projects/crates/cobblegame.lua:132>

             ?: in function ‘dispatchEvent’

             ?: in function ‘?’

             ?: in function ‘gotoScene’

             …documents/corona projects/crates/cobblegame.lua:89: in function ‘_onRelease’

             ?: in function ‘?’

             ?: in function <?:1091>

             ?: in function <?:218>

[/lua]

Again, thanks for helping me. I did replace the ‘infinite’ with zero, thanks for telling me about that (i’m still a noob at programming).

EDIT: I fixed it, I had to add a ‘nil’ command after removeself, like this:

[lua]

    for i=1, #blocks do

    blocks[i]:removeSelf()

    blocks[i] = nil

    end

[/lua]