EXC_BAD_ACCESS error and strange behaviour when mo

2019-09-06 10:38发布

问题:

I am having a few issues with making a box2d body move in a straight line. At times when I run the code it works (body moves in a straight line) and other times it falls as it moves. Below is the scheduleSprite method which I call in the update method.

-(void)scheduleSprite {

if ([testSprite physicsSprite].b2Body != NULL) {
    b2Vec2 force = b2Vec2(-5, 10.0 * [testSprite physicsSprite].b2Body->GetMass());
    [testSprite physicsSprite].b2Body->ApplyForce(force, [testSprite physicsSprite].b2Body->GetWorldCenter() );
}
if ([testSprite physicsSprite].position.x < 0) {
    world->DestroyBody([testSprite physicsSprite].b2Body);
    [testSprite physicsSprite].b2Body = NULL;
}
}

Also, when the second if condition is satisfied I get an EXC_BAD_ACCESS pointing to line

b2Vec2 pos  = _b2Body->GetPosition();

in the

-(CGAffineTransform) nodeToParentTransform

method, within the class

CCPhysicsSprite.mm

It would appreciate any assistance.