游戏崩溃,当我试图摧毁b2Body,我该怎么办? [英] Game crashes when I try to destroy a b2Body, what should I do?

查看:123
本文介绍了游戏崩溃,当我试图摧毁b2Body,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是控制台中崩溃的原因。

That is what it the crash leaves in the console.

[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);

我认为我做正确的东西..

I think im doing the right stuff..

@property (nonatomic, assign) b2Body *body;

这是我如何使它一个属性

Here is how i "make it" a property

我不明白为什么它不工作,身体是一个适当的指针,因为我可以从bodys UserData像在身体的肌酸中设置的标签检索信息,所以应该是一个问题..任何人知道我的代码有什么问题?

I dont understand why it doesnt work, "body" is a proper pointer because I can retrieve infromation from the bodys UserData like tags that are set in the creatin of the body, so that shouldnt be a problem.. Do anyone know whats wrong with my code?

谢谢。

编辑:

-(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?

    } 


    }

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

Is it not outside the timestep? Please help me here...

谢谢

推荐答案

必须扫描联系人,将所有联系人存储在阵列中,然后在所有联系人都检查后,删除您的身体。

You must scan for contacts, store all contacts in an array, and then AFTER all contacts have been checked, you remove your bodies.

检查联系人:

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);
        }
    }
}

请注意,随机的代码块我已经复制和粘贴。他们只是为了例子,如果你尝试读它们作为确切的指令,可能只会混淆你。

Take note that these are just random chunks of code I have copy and pasted in. They are for example only and may only confuse you if you try to read them as exact instructions.

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

The point here is: You can not remove a body while it is being accessed by the step or contact listener. Finish using the contact listener and then remove your bodies.

这篇关于游戏崩溃,当我试图摧毁b2Body,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆