I have this piece of code to destroy an object if it goes below a certain point. This code runs every frame in the "onEnterFrame" function.
for i, val in pairs(self.balls) do
if val:getY() > 500 then
self.world:destroyBody(val.body)
end
end |
However, whenever the object goes below that point, an error comes up saying:
Scenes/game.lua:74: Body is already destroyed.
I tried adding code that checks if val.body is nil, and doesn't destroy it if so, but the error message still came up. Does anybody know why this is happening?
Comments
[-] Liasoft
http://docs.giderosmobile.com/reference/physics/b2World/destroyBody#b2.World:destroyBody
So another solution if you are not going to need the object anymore is:
[-] Liasoft
It's good you have found your own solution, but just out of curiosity is using your own script or, are you using Box2dEasy or any? If you are using your own script, how are you definied running the world (EnterFrame)? for future references.
[-] Liasoft
Could you perhaps write a short, complete project which illustrates the problem and gives the error.
In Nebula Retro, I do the same thing: destroy crates when they fall off the screen (ie I destroy both the Sprite and the body), I didn't get an error, see here for how I did it:
https://github.com/john-blackburn/nebularetro/blob/master/main.lua#L1079
This function is called on ENTER_FRAME as well.
The .body field has been set up earlier (manually by me, it is not a standard Sprite field)
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
Likes: antix, john26
[-] Liasoft
This is the way (IMHO) to work with tables of game objects.. pairs is slow.
[-] Liasoft