Question

Please help me, i am trying to remove b2Body from world but getting assertion error as "Assertion failed: (m_world->IsLocked() == false)" in the following code:

-(void)beginContact:(b2Contact *)contact{
for (int i=0; i<10; i++) {
    b2Body *bodyA=contact->GetFixtureA()->GetBody();
    b2Body *bodyB=contact->GetFixtureB()->GetBody();
    if((bodyA&&bodyA==monsterBody[i])||(bodyB&&bodyB==monsterBody[i]))
    {   [self removeChild:(CCSprite*)monsterBody[i]->GetUserData() cleanup:YES];
        NSLog(@"%d",_world->IsLocked());
        _world->DestroyBody(monsterBody[i]);
        break;}
}}
Était-ce utile?

La solution

Contact listener callback methods are executed during (within) a world step time. So deleting a body at this time will cause Assertion failed error.

What you can do is set a bool like isOkToDelete in the body's user data. Then inside the beginContact() callback just update the bool to yes. And then you can do delete outside the step method like even inside the update() tick after cocos2d renders the sprite of the body, or you can insert the body into an array and delete later etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top