2
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ I guess I found the answer. The CreateFixture and DestroyFixture methods are locked during callbacks. Just let me test it. \$\endgroup\$ – Henrique Müller Mar 20 '14 at 17:07
  • \$\begingroup\$ 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. \$\endgroup\$ – bummzack Mar 20 '14 at 17:26
  • \$\begingroup\$ Many thanks! Actually, now I've already taken another approach (worse than yours). But next time I already know what to do! \$\endgroup\$ – Henrique Müller Mar 20 '14 at 17:55
  • \$\begingroup\$ 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.) \$\endgroup\$ – Anko Mar 20 '14 at 18:38
  • \$\begingroup\$ Ok. Thanks for the corrections. English is not my first language. \$\endgroup\$ – Henrique Müller Mar 20 '14 at 18:40
2
\$\begingroup\$

After all, everything was (almost) correct. The problem is you can't change the physics during callback. All I had to do was delay it to the next game cycle.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.