Insights needed on non trivial physics object removal (from more than one source)

Here’s what I have:

A have a table that I use to create multiple balls, all with physics bodies and different properties.

In my set up function I have this:
[lua]Ball={}
for i = 1, BallNumber do
Ball[i] = display.newImageRect(BallImage, 32, 32)
Ball[i].x = math.random(11,469); PUP[i].y = math.random(-100,-50)*i
physics.addBody( Ball[i], { density=math.random()*.8 + .6, friction = 0.2, bounce = 0,3, radius = 16 } )
Ball[i].bodyType = “dynamic”
Ball[i].linearDamping = math.random()*.4 + 0.3
– and a few more[/lua]

This works well.

When the balls collide with a hole I want them to disappear and perform something according to their type, if they collide with the ground I just want them to disappear. This also works perfectly. This is the relevant part of collision function attached to the ground and hole:

[lua]onLocalCollision = function( self, event )
if BallTimer>0 then
if ( event.phase == “began” ) then
if self.id == “Hole” then
–Activate Ball’s properties
end
BallIndex = event.other.number

if (event.other.number ~= nil and BallTimer>0) then
event.other:removeSelf()
collectgarbage(“collect”)
BallNumber = BallNumber - 1
end
end

end[/lua]
Basically it removes the ball, activates if appropiate and gets it’s index on the table so I can reuse it. It decreases the number of balls so I can check on my enterframe that if it’s below the max I create a new ball. I use the same function as above but instead of [i] I use Ball[BallIndex]. Works flawlessly.

You might be wondering what that BallTimer stuff was all about. And here’s when the problems start. I have a countdown timer. When it reaches zero I want to remove all the balls. This is proving very challenging. I’ve tried everything I can think off. I’m constantly checking stuff is not nil, before removing, but I’m removing nil stuff anyways. I set up a timer.performWithDelay to call a remove function, but it still doesn’t work. The problem seems to be that the collision function is removing a ball when the timer hits zero and it removes it too, at the same time. Delaying the timer doesn’t do me any good, because a collision could still be firing. I’ve removed the collision listeners when the timer hits zero, but that doesn’t work either.

My last attempt as been to set them as isVisible = false. And then remove them on the gameover screen, which happens a lot later (when the timer gets to zero you still get one more chance to play by design, so no immediate gameover). This works ok on the simulator, even though the invisible objects seem to still have mass and move other stuff around as they collide (but no collision events fire). However on the device I get weird stuff happening, there is almost certainly a runtime error there (almost everytime too, I just can’t replicate it on the simulator).

This is my remove code:

[lua]function Ballremove()
for i=1, BallNumber do
if PUP[BallNumber-i]~=nil then
PUP[BallNumber-i]:removeSelf()
PUP[BallNumber-i]=nil
end
end[/lua]
I’m removing them backwards like it’s suggested in the docs. So I’m running out of ideas here. I should mention some of the balls effects is to decrease the timer. But still, it’s not supposed to remove balls on collision if the timer is below zero, nor create new ones.

I’d love to hear some insight from some of you on how to deal with this if you managed to read through all that. Thanks. [import]uid: 10835 topic_id: 5576 reply_id: 305576[/import]

Mmmm weird.

I was using the joystick code from the code samples to test tilt on the simulator. I removed it and removed the multitouch requirement since I didn’t need it (but was required for that code, that I didn’t really look over, just needed something quick and dirty), and I haven’t seen any weird behavior yet.

Maybe it was one of those Corona bugs I said Ansca shouldn’t hurry to fix before adding iAds and Game Center. I guess it serves me right, LOL (though I did find a workaround apparently, like I always said I would).

Edit: I’m still interested in insight on how to do this better/cleaner. [import]uid: 10835 topic_id: 5576 reply_id: 18943[/import]