Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot destroy the body(Box2d)

Tags:

box2d

- for (var bb1:b2Body= world.GetBodyList(); bb1; bb1 = bb1.GetNext())
  {
     if (bb1.GetUserData() is Sprite)
     {
         world.DestroyBody(bb1);
     }
  }
  world=null;

is it correct to remove the b2body in box2d?

but still i'm seeing the objects in stage.

like image 673
game_app Avatar asked Feb 21 '23 23:02

game_app


1 Answers

If you are trying to do this inside the world's Step() function (eg in a contact listener), it will not work because the world is still processing the bodies. You will need to make a note of which bodies you want to destroy, and then destroy them after the world's time step has finished.

Also, I'm not sure what language you are using but it seems a little strange that this loop will destroy a body and then call GetNext() on the thing you just destroyed.

like image 130
iforce2d Avatar answered Mar 07 '23 03:03

iforce2d