DestroyBody not working with userData

Hello,

I just pulled my hair out over this problem and was hoping to save other AS3 developers' hair. Sorry if this solution is a duplicate.

I'm using the AS3 port of Box2D. If DestroyBody is not working and you're using userData, try setting the userData to null just before calling DestroyBody:

Code:
...

var o = this.getBodyFromUserData(mc);
if(null != o)
{
   o.m_userData = null;
   this.world.DestroyBody(o);
}

...

public function getBodyFromUserData(mc:MovieClip):b2Body
{
   for (var b:b2Body = this.m_world.GetBodyList(); b; b = b.GetNext())
   {
      if(b.m_userData == mc)
      {
         return b;
      }
   }
   
   return null;
}


Before "o.m_userData = null" was added, the shape was still available for contact filtering.

If you're not using userData and DestroyBody is not working, it's time for a "reference audit".


-Mike
maybe you try this


var o = this.getBodyFromUserData(mc);
if(null != o)
{
this.world.m_lock = false;
o.m_userData = null;
this.world.DestroyBody(o);
}
ah!
this helped me lot!
thanks.

one update though:

its not body.userData=null

but

body.SetUserData(null)

cheerio!
Hi, how are you?

I'm having a similar problem, and I tried setting the m_lock to true, but the physics no longer apply when I do this, I mean, the object is removed, but the ball doesn't bounce with the brick, it goes right through it.

This is my code:
Code:
shape.parent.removeChild(shape);
PhysiVals.world.m_lock = false;
PhysiVals.world.DestroyBody(body);


If I don't do the lock = false, the ball bounces, and the sprite is removed, but not the body...

any Ideas?

Thanks

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