当我试图摧毁b2Body时游戏崩溃,我该怎么办?

时间:2011-06-22 17:14:15

标签: iphone objective-c cocos2d-iphone box2d

"Assertion failed: (m_bodyCount < m_bodyCapacity), function Add, file libs/Box2D/Dynamics/b2Island.h, line 65." 

这就是崩溃在控制台中留下的内容。

[self removeChild:(CCSprite*)body->GetUserData() cleanup:YES];

body->SetTransform(b2Vec2(30, 30), 0); //moving the body out of the scene so it doesnt collide anymore!

 world->DestroyBody(body);

我认为我正在做正确的事情......

@property (nonatomic, assign) b2Body *body;

以下是我如何“成为”财产

我不明白为什么它不起作用,“body”是一个正确的指针,因为我可以从身体的肌肉中检测信息,就像在身体肌酸中设置的标签一样,所以这不应该是一个问题..有谁知道我的代码怎么了?

谢谢。

编辑:

-(void) tick: (ccTime) dt //Main loop
{

if (ballFired) {
    Magnet *aMagnet = [magnetArray objectAtIndex:0];

    world->DestroyBody(aMagnet.body); //It crashes here!
}



int32 velocityIterations = 8;
int32 positionIterations = 1;

// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);


//Iterate over the bodies in the physics world
for (b = world->GetBodyList(); b; b = b->GetNext())
{
    if (b->GetUserData() != NULL) {
        //Synchronize the AtlasSprites position and rotation with the corresponding body
        CCSprite *myActor = (CCSprite*)b->GetUserData();
        myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b>GetPosition().y * PTM_RATIO);
        myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
    }   
    }




}

这不是世界上的一步吗?

编辑2:

        ContactListener::ContactListener(){};

         void ContactListener::BeginContact(b2Contact* contact)
        {
        // Box2d objects that collided
       b2Fixture* fixtureA = contact->GetFixtureA();


       b2Fixture* fixtureB = contact->GetFixtureB();
       CCSprite* actorA = (CCSprite*) fixtureA->GetBody()->GetUserData();
       CCSprite* actorB = (CCSprite*)  fixtureB->GetBody()->GetUserData();



      if(actorA == nil || actorB == nil) return;

    b2WorldManifold* worldManifold = new b2WorldManifold();
    contact->GetWorldManifold(worldManifold);


      Kollisjon *kollisjon = [Kollisjon sharedKollisjon];

     if (actorA.tag == 1) {

    NSLog(@"OK1");

    kollisjon.kollidertBody = fixtureB->GetBody();

    kollisjon.world->DestroyBody(kollisjon.kollidertBody); //Isnt this ok?

    }

    else if (actorB.tag == 1) {

       NSLog(@"OK2");


       kollisjon.kollidertBody = fixtureA->GetBody();

       kollisjon.world->DestroyBody(kollisjon.kollidertBody); //Isnt this ok?

    } 


    }

它不在时间步之外吗?请帮帮我......

谢谢

1 个答案:

答案 0 :(得分:3)

您必须扫描联系人,将所有联系人存储在一个数组中,然后在检查完所有联系人后,删除您的身体。

检查联系人:

std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); 
    pos != _contactListener->_contacts.end(); ++pos) 
{
    MyContact contact = *pos;

    b2Body *bodyA = contact.fixtureA->GetBody();
    b2Body *bodyB = contact.fixtureB->GetBody();

    // Rocket explosion rect
    if(bodyA->GetUserData() == NULL)
    {
        NSLog(@"NULL collision detected. (BODY A)");

        hasDoneRocketCollisions = YES;

        CCSprite *sprite = (CCSprite*) bodyB->GetUserData();

        if(sprite.visible == NO) continue;

        if(sprite.tag >= 200 && sprite.tag < 300)
        {
            index = sprite.tag - 200;
            if([spriteTracker containsObject:sprite]) continue;
            [spriteTracker addObject:sprite];
            bodiesToKill[counter] = bodyB;
            [enemyChargerIsAlive replaceObjectAtIndex:(int)(sprite.tag-200) withObject:[NSNumber numberWithInt:0]];
            [ParticleController spawnExplosion:sprite.position inParent:currentDefaultNode];
        }
        else if(sprite.tag >= 300 && sprite.tag < 400)
        {
            index = sprite.tag - 300;
            if([spriteTracker containsObject:sprite]) continue;
            [spriteTracker addObject:sprite];
            bodiesToKill[counter] = bodyB;
            [enemyShooterIsAlive replaceObjectAtIndex:(int)(sprite.tag-300) withObject:[NSNumber numberWithInt:0]];
            [ParticleController spawnExplosion:sprite.position inParent:currentDefaultNode];
            counter++;
        }
    }
}

在检查完所有联系人后,在您的方法中稍后:

b2Body *dyingBody;

for(int i = 0; i < counter; i++)
{
    CCSprite *dyingSprite;
    dyingSprite = [spriteTracker objectAtIndex:i];
    dyingSprite.visible = NO;

    // Is player projectile
    if(dyingSprite.tag >= 100 && dyingSprite.tag < 200)
    {
        CCParticleSystemQuad *dyingParticle;
        dyingParticle  = [particlesToKill objectAtIndex:particleIndex];
        particleIndex++;
        [dyingParticle stopSystem];

        dyingBody = bodiesToKill[i];
        dyingBody->SetActive(false);

        [ParticleController spawnExplosion:dyingSprite.position inParent:currentDefaultNode];
        [AudioController playExplosion];

        dyingSprite.visible = NO;

        if([_player currentShotType] == 1)
        {
            rocketHitBody->SetTransform(b2Vec2(dyingSprite.position.x/PTM_RATIO, dyingSprite.position.y/PTM_RATIO), 0.0);
            rocketHitBody->SetActive(true);
        }
    }
}

请注意,这些只是我复制和粘贴的随机代码块。仅作为示例,如果您尝试将其作为确切说明阅读,可能只会让您感到困惑。

这里的要点是:在步骤或联系人侦听器访问正文时,您无法删除正文。完成使用联系人侦听器,然后删除您的身体。