从复制的集合中销毁box2d关节

时间:2012-10-20 07:48:54

标签: c++ cocos2d-iphone box2d box2d-iphone

我正在尝试创建一个方法(destroyWeldJoint)来破坏复制集中的焊接接头。关节最初是在另一个方法(createWeldJoint)中创建的。我在另一种方法中使用试图破坏焊接接头的原因是因为我在更新方法中调用createWeldJoint,我想避免在枚举时尝试更改集合。请参阅以下代码:

createWeldJoint:

-(void) createWeldJoint {

// using the iterator pos over the set
std::set<BodyPair *>::iterator pos;

for(pos = bodiesForJoints.begin(); pos != bodiesForJoints.end(); ++pos)
{

    b2WeldJointDef      weldJointDef;

    BodyPair            *bodyPair = *pos;
    b2Body              *bodyA = bodyPair->bodyA;
    b2Body              *bodyB = bodyPair->bodyB;


    weldJointDef.Initialize(bodyA,
                            bodyB,
                            bodyA->GetWorldCenter());
    weldJointDef.collideConnected = false;
    weldJoint = (b2WeldJoint*) world->CreateJoint(&weldJointDef);

    // Free the structure we allocated earlier.
    free(bodyPair);

    // Remove the entry from the set.
    //bodiesForJoints.erase(pos);

}

// copy of bodiesForJoints
std::set<BodyPair *> bodiesForJointsCopy(bodiesForJoints.begin(), bodiesForJoints.end());

bodiesForJoints.clear();

}

destroyWeldJoint:

- (void) destroyWeldJoint    {
std::set<BodyPair *>::iterator pos;

for(pos = bodiesForJointsCopy.begin(); pos != bodiesForJointsCopy.end(); ++pos)
{

    BodyPair            *bodyPair = *pos;
    b2Body              *bodyA = bodyPair->bodyA;
    b2Body              *bodyB = bodyPair->bodyB;

    // weldjoint should be destroyed here


    // Free the structure we allocated earlier.
    free(bodyPair);

}    
}

如何在destroyWeldJoint方法中销毁weldjoint(在createWeldJoint中)?

0 个答案:

没有答案