I created lots of physics bodies using box2d and tiledmap for a runner type game.
My Actor is named as body1
My coins are namedd dashingN (N = count)
My obstacles are named as demonX (X = level number)
My problem is once I try to finish the level or say gameover, I need to destroy all the bodies so that I can create new world for new level.
But I could not destroy the objects as always I get Body is alredy destroyed in few parts of code always whenever I try to destroy.
Can anybody help me solve this
Is there any easy way to do this instead of box2d??
Comments
I figured it myself.
I was creating world in mainmenu.lua, which I moved to my level screen and then iteratively removed all objects and bodies on game over which worked.
I created the below method in my MapLayout.lua
destroyAll()
self.destroyCalled=true
for i=1, #self.bodies do
if self.bodies[i] then
world:destroyBody(self.bodies[i])
end
end
self:removeFromParent()
end
Actually it is a bit of a pain that destroyed box2d objects still remain but can not be destroyed for the second time and can not be checked if they are already destroyed.
So you need to maintain very strict index of them or apply different hacks.
I've was struggling with something similar but only with joints. When two bodies are merged, the reference to both of them was stored as property of body, but then if one body get's destroyed, joint also get's destroyed, and when you try (for some reason) destroy the joint, there is an error it's already destroyed.
The easiest hack I've found to set a property on joint as joint.isDestroyed = true this way you'll know if it was already destroyed or not.
I know offtopic as usually, but maybe someone finds it useful