0

我创建了一个weldJoint 并将其销毁(请参阅下面代码中的更新方法)。在破坏焊接接头时,我设置了线速度以使一个物体朝向另一个物体,从而创建另一个焊接接头。焊缝永远不会被重新创建。我确信这是因为以下几行:

if (weldJoint == NULL) return;

world->DestroyJoint(weldJoint);
weldJoint = NULL;

在代码中。因为在下一个时间步中它已经为 NULL,所以它不会创建焊接接头。起初我在破坏关节后尝试使用 break 语句,但我得到一个指向该行的 EXC_BAD_ACCESS 错误:

world->DestroyJoint(weldJoint);

如何创建销毁并重新创建焊接接头?

更新方法:

-(void) update: (ccTime) dt
{

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

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

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext())    {
if (b->GetUserData() != NULL)
{
    CCSprite *mainSprite = (CCSprite*)b->GetUserData();
    if (mainSprite.tag == 1) {
        mainSprite.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
        CGPoint mainSpritePosition = mainSprite.position;
        if (mainSprite.isMoved) {

                    if (weldJoint == NULL) return; 

                    world->DestroyJoint(weldJoint);
                    weldJoint = NULL;

                    b2Vec2 velocity = b2Vec2(3.5, 2.2);
                    b->SetLinearVelocity(velocity);  


                }
        }
    }
}

}  
4

0 回答 0