在cocos2d中破坏box2d体

时间:2013-08-27 10:32:13

标签: ios cocos2d-iphone box2d-iphone

我有一个网和一些昆虫我扔网和与昆虫碰撞检测并摧毁它们。我使用bod2d进行碰撞检测。

世界方法

-(void)createWorld
{

 // Define the gravity vector.
    b2Vec2 b_gravity;
    b_gravity.Set(0.0f, -9.8f);

 // Do we want to let bodies sleep?
 // This will speed up the physics simulation
    bool doSleep = true;

 // Construct a world object, which will hold and simulate the rigid bodies.
    world = new b2World(b_gravity);
    world->SetAllowSleeping(doSleep);

    world->SetContinuousPhysics(true);

}

创建网站

-(void) createWeb
{
    freeBodySprite = [CCSprite spriteWithFile:@"web1.png"];
        [self addChild:freeBodySprite z:2 tag:TAG_WEB];

    CGPoint startPos = CGPointMake(100, 320/1.25);

    bodyDef.type = b2_staticBody;
    bodyDef.position = [self toMeters:startPos];
    bodyDef.userData = freeBodySprite;

    float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 1.0f);
    shape.m_radius = radiusInMeters;

    fixtureDef.shape = &shape;
    fixtureDef.density = 0.01f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0.1f;

    circularObstacleBody = world->CreateBody(&bodyDef);
    stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
        freeBody = circularObstacleBody;
}

我用spritesheet创建了我的昆虫动画,然后为该Sprite创建了b2body

-(b2Body *) createMovingBoxObstacle
{


    //set this to avoid updating this object in the tick schedule
        _ants.userData = (void *)YES;

    b2BodyDef bodyDef_Ant;
    bodyDef_Ant.type = b2_dynamicBody;
    CGPoint startPos = ccp(520,winSize.height/7.6);
    bodyDef_Ant.position = [self toMeters:startPos];
    bodyDef_Ant.userData = _ants;

    b2PolygonShape dynamicBox;
    float tileWidth = ((_ants.contentSize.width * _ants.scale/PTM_RATIO) * 0.5f);
    float tileHeight = ((_ants.contentSize.height * _ants.scale/PTM_RATIO) * 0.5f);
    dynamicBox.SetAsBox(tileWidth, tileHeight);

    b2FixtureDef fixtureDef_Ant;
    fixtureDef_Ant.shape = &dynamicBox;
    fixtureDef_Ant.friction = 0.7;
    fixtureDef_Ant.density = 0.1f;
    fixtureDef_Ant.restitution = 0.7;


    staticBody = world->CreateBody(&bodyDef_Ant);
    staticBody->CreateFixture(&fixtureDef_Ant);

       VAMovingObstacle* moveableObject = [[VAMovingObstacle alloc] init];
       moveableObject.startPoint = ccp(520,winSize.height/7.6);
       moveableObject.endPoint = ccp(-50,winSize.height/7.6);
       moveableObject.transitionTime = 20.0;
       moveableObject.breakTime = 1.0;

    moveableObject.obstacleSprite = _ants;
    moveableObject.physicalBody = staticBody;
    [moveableObject startMovement];

    if (!movingObstacles) {
        movingObstacles = [[NSMutableArray alloc] init];
    }
    [movingObstacles addObject:moveableObject];
    [moveableObject release];
    return staticBody;
}

VAMovingObstacle是一个帮助sprite和b2body从左向右移动(runaction)的类

这是我的联系人列表器EndContact方法

void ContactListener::EndContact(b2Contact* contact)
{
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
        CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
        CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();


        if (spriteA.tag == TAG_WEB && spriteB.tag >= TAG_ANT) {

        }

        else if (spriteA.tag >= TAG_ANT && spriteB.tag == TAG_WEB) {

            [spriteA removeFromParentAndCleanup:YES];
            [[HelloWorldLayer sharedLevel] WebCollisionWithInsect:bodyA];
        }
    }
}

发生碰撞时EndContact方法调用WebCollisionWithInsect:bodyA

-(void) WebCollisionWithInsect:(b2Body*) bodyT{
            world->DestroyBody(bodyT);
}

在world-> DestroyBody(bodyT)行上,它会给出错误

  

断言失败:(IsLocked()== false),函数DestroyBody,文件   /Users/libs/Box2D/Dynamics/b2World.cpp,第134行。

任何想法我做错了什么。 ?

被修改 我在EndContact中添加了这一行

destroyCollisionDetectionBody = objBody;
didInsectCollideWithWeb = YES;

保持身体的参考我想破坏。 现在,如果检查,我可以在tick方法内部破坏我的身体 在tick方法中添加了这些行

if(didInsectCollideWithWeb)
    {
        [self unschedule:@selector(tick:)];
        [freeBodySprite removeFromParentAndCleanup:YES]; // this is my web
        world->DestroyBody(destroyCollisionDetectionBody);
        world->DestroyBody(freeBody);
        [self createWeb];
        [self schedule:@selector(tick:)];
        didInsectCollideWithWeb = NO;

    }

但现在的问题是,当我正在摧毁我的网络,即freebody。我不能再创造它。正如我之前所做的那样,通过调用[self createWeb];

1 个答案:

答案 0 :(得分:1)

您正在尝试在模拟阶段使用box2d对象时将其销毁(在此阶段调用EndContact)。模拟阶段是您调用世界step方法时执行的阶段。

所以你应该做的是保持对你要删除的body对象的引用,并在box2d模拟阶段之后(在step方法返回之后)执行带有所需正文的WebCollisionWithInsect