Batch remove physics objects

I have some physics objects which I want to remove when my game over function is called. I loop through my data table (refuse) and try to remove them if they exist on the screen. I tried removing the group that they are all in but if an object is touched before and released after the game over my app crashes, presumably because the touch event is unresolved. Here is some code, please help me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-------------------
function fadeOutDestroy( body )
    local body = body
    local destroy = function()
        body:removeEventListener( "touch" , dragRefuse )
        body:removeSelf()
        body = nil
    end
    transition.to( body, { time=500, delay=500, alpha=0, onComplete=destroy } )    
end
-------------------
--> loop through the refuse data table and remove objects from screen 
for i=1, #refuse do
    if refuse[i].object then
        local body = refuse[i].object
        print ( body.name )
        fadeOutDestroy( body )
    end
end
-------------------

It looks like when you are calling the "fadeOutDestroy" function the body is being removed within the "destroy" function and then the transition.to is attempting to call "destroy" again at which point the body had already been removed so it contains nil. Try removing the "onComplete=destroy" call from the transition.

But the local 'destroy' function only gets called within my 'fadeOutDestroy' function when the alpha transition has been completed. If I remove the onComplete call surely that would just fadeout all the objects but not actually remove anything from the physics engine or their touch event listeners ?

My apologies. You are correct. I guess I shouldn't try to answer questions until after the caffeine kicks in. The only thing that I can see is that there might be a conflict with using the name "body" for two different tables. Maybe try renaming the local inside "fadeOutDestroy" to something like "tempBody" instead of "body".

not sure if this will help but try looping backwards..

1
2
3
4
5
6
7
for i=#refuse, 1, -1 do
    if refuse[i].object then
        local body = refuse[i].object
        print ( body.name )
        fadeOutDestroy( body )
    end
end
views:1597 update:2011/9/29 19:21:19
corona forums © 2003-2011