Removing an Actor

So, I can remove the body from the world, but it doesn't get rid of the actor Sprite attached to it. What does the body do with the m_userData? Is there an easy way to remove it from the display list?

Thanks!
Chris
If you have your m_userData set to a Sprite, I would recommend calling removeChild on the Sprite, then setting the m_userData to null, and then destroying the body.

I would imagine that the body loses it's reference (userData) to the Sprite, but the Flash display list still contains a reference, which is what the removeChild is for. It may not actually be necessary to null userData, but I have not actually looked at the code that is run when a body is destroyed.
Ah yes... That seems to work now! Basically, I lost reference to it when I destroyed the body. The physics stopped but the sprite was still just there. Thanks!
hi,

i have somthing to do with DestroyBody that i'm not sure how to deal with,
its more my lack of AS3 knowledge of Iterators...

here i have a function that is supposed to destroy my bounds when screen is resized,

Code:
public function destroyBounds(event:Event = null):void
{
       for (var bb:b2Body = Box2D_PhysicsEngineModel.m_world.m_bodyList; bb; bb = bb.m_next)
      {   
              if ((bb.m_userData.name == "leftBoundMC" || bb.m_userData.name == "rightBoundMC" || bb.m_userData.name =="bottomBoundMC"))
   {
      _stage.removeChild(bb.m_userData);
      bb.m_userData = null;
      Box2D_PhysicsEngineModel.m_world.DestroyBody(bb);
               
   }         
      }
      buildBounds();
}

the removing of the SPRITES work fine,
and i null it after removal, but whn i try destroy the body from the engine,
it only removes the first time this iterator is executed.
is because bb is actually the m_bodyList itself, so i am deleting the list?
how would i code this so that it removes the current element, and logically move the pointer to the next list element (AS3)?
or maybe target the current b2Body the iterator is at?

thanks
Clark
Well you are not deleting the list, m_bodyList is just a reference to the first body in a linked list that belongs to the b2World object. When you call DestroyBody it actually rearranges the linked list references for you. So it is removing all the bodies correctly, and then you create them again in their new position, and they don't remove the second time? Is the BuildBounds() function you are calling used to create the bounding boxes originally as well? Also, it would probably be easier if you just kept a reference to the bounding boxes when you first create them, that way you don't even have to iterate through the body list.

Code:
var boundBox1:b2Body = Box2D_PhysicsEngineModel.m_world.CreateBody(yourBodyDef);
//etc...


Or you could just keep a small array which would be a little less code.

I am not clear on whether only the first bounding box is being destroyed, or they are all only being destroyed the first time. Are you sure the m_userData and names are being set right?

If you post any more code could you please use the "Code" button above the box where you enter your post, it makes the formatting come through better 🙂 Let me know if this helps.
brainclog!,

whos the man! who's the man! u are. *unless u're a lady. hi5. very nice!!!
thanks for that, i took your advise and got it working...

i used an array to put all my objects AND references to b2Body in a 2d array.
and then on resize, just delete the sprite elements in the array, and call DestroyBody as well on the b2Body elements
then it seems before i call build again, i have to clear the array.
Code:
_boundsArray = new Array();


and it works like a charm. and thanks for the explaination of linked lists, i'm a bit rusty from forgetting a lot of C++..

cheers
Clarklin
Cool, glad you were able to get it working!

Original thread: https://box2d.org/forum/viewtopic.php?t=306