# Why can't I swap fixtures on collision in Box2D?

I'm trying to swap the fixtures associated with a body after colliding with a specific obstacle.

I can listen to the collision event by extending b2ContactListener. I tested it with a trace and it's working. My listener function in the main game class calls a Player method that makes the swap. But it's just not working.

The body is stored in public var _body:b2Body; in the Player class.

The method I'm calling goes like this:

public function changeFixture():void {
var newShape:b2CircleShape = new b2CircleShape(1);
var myFixtureDef:b2FixtureDef = new b2FixtureDef();
// Here I set density, friction, restitution, etc... Not important now...
myFixtureDef.shape = newShape;

_body.DestroyFixture(_oldFixture);
_body.CreateFixture(myFixtureDef);

// I know the method is called because this works
trace('Yay! I'm working!');
}


What might be wrong?

• I guess I found the answer. The CreateFixture and DestroyFixture methods are locked during callbacks. Just let me test it. – Henrique Müller Mar 20 '14 at 17:07
• Yes, that is the case. You can't change your physics world during collision-callbacks. Store all the recorded collisions in a list and process the list of entities after the physics update. – bummzack Mar 20 '14 at 17:26
• Many thanks! Actually, now I've already taken another approach (worse than yours). But next time I already know what to do! – Henrique Müller Mar 20 '14 at 17:55
• By the way, it's OK to answer your own question and mark it accepted. (It's useful for future readers with the same problem.) – Anko Mar 20 '14 at 18:38
• Ok. Thanks for the corrections. English is not my first language. – Henrique Müller Mar 20 '14 at 18:40