Prevent the game from crashing when >2 collisions happen at same time

[lua]function onCollision(event )
print(“collision”)

local function realCollisionHandler()
if(event.object1.x <0 and event.object1.y<0) then
event.object1:removeSelf()
event.object2:removeSelf()
print(“remove”)
else
print(event.object1.contentWidth)
print(event.object1.contentHeight)
print(event.object2.contentWidth)
print(event.object2.contentHeight)

local vx2, vy2 = event.object2:getLinearVelocity()
local vx1, vy1 = event.object1:getLinearVelocity()

local rock = display.newCircle((event.object1.x + event.object2.x)/2,(event.object1.y + event.object2.y)/2,(event.object1.contentHeight + event.object2.contentHeight)/3 )

physics.addBody(rock,“dynamic”,{friction = 0,bounce = 0})

rock:setLinearVelocity( (vx1+vx2)/2,(vy1+vy2)/2)
event.object1:removeSelf()
event.object2:removeSelf()
end
end
–physics.addBody(rock,“dynamic”,{friction = 0,bounce = 0})

timer.performWithDelay(1,realCollisionHandler,1)
–myGroup = nil

end[/lua]

My simulator will spawn random objects and they will merge when collide with each other. However, when more than 2 collisions happen at the same time, it will crash. Could some please give me a suggestion how to fix this?

My original response is posted below and still true, but I realised that your problem is simpler than that…

You are not returning true, so you are trying to remove both objects in the collision twice, so the second removeSelf is invalid because it has already occurred. ‘return true’ at the end and handle the merge only once and you should be fine.

There are other lessons here, however, so here is my original response…

Without seeing the error that you get its kinda hard to know what to suggest, however you mention that the error occurs when you have more than 2 objects colliding, but you also try to remove both objects after a short delay. I would expect that 2 objects collide, then another object collides with one of the first two objects within that short delay time.

So, what you really want to do is only try to merge the first two objects and ignore them when the third object collides with one of them. This is because the code to remove the first object gets called twice, meaning you are probably getting an error telling you that it has been removed already. I suggest setting a flag on an object within the onCollision function, right at the top, before calling the timer, and inside the realCollisionHandler function you need to check that flag - if it is true, do not try to remove that object. In fact, don’t do anything with it.

My original response is posted below and still true, but I realised that your problem is simpler than that…

You are not returning true, so you are trying to remove both objects in the collision twice, so the second removeSelf is invalid because it has already occurred. ‘return true’ at the end and handle the merge only once and you should be fine.

There are other lessons here, however, so here is my original response…

Without seeing the error that you get its kinda hard to know what to suggest, however you mention that the error occurs when you have more than 2 objects colliding, but you also try to remove both objects after a short delay. I would expect that 2 objects collide, then another object collides with one of the first two objects within that short delay time.

So, what you really want to do is only try to merge the first two objects and ignore them when the third object collides with one of them. This is because the code to remove the first object gets called twice, meaning you are probably getting an error telling you that it has been removed already. I suggest setting a flag on an object within the onCollision function, right at the top, before calling the timer, and inside the realCollisionHandler function you need to check that flag - if it is true, do not try to remove that object. In fact, don’t do anything with it.