2

I have created a revolute joint between two bodies. When a another body gets into contact (see Contacter.mm below) with one of them (body with tag ==90), I destroy the body with tag == 90. I put a CCLOG to track the sprite position of the revolute joint body that was not destroyed. The CCLOG works fine till it's joint body is destroyed. Then the CCLOG stops printing to the console. How can I solve this? Below is the relevant code.

Contacter.mm:

#import "Contacter.h"
#import "Box2D.h"

@implementation Contacter

@synthesize arrayOfBodies = _arrayOfBodies;
@synthesize spriteToDestroy = _spriteToDestroy;

-(void)destroyBodies:(b2Body*)body   {
_arrayOfBodies = [[NSMutableArray alloc] init];
NSValue *bodyValue = [NSValue valueWithPointer:body];
[_arrayOfBodies addObject:bodyValue];
}

-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite (CCPhysicsSprite*)twoPhysicsSprite; {
int firstTag = onePhysicsSprite.tag;
int secondTag = twoPhysicsSprite.tag;

if (((firstTag == 90) && (secondTag == 101 )) || ((firstTag == 101) && (secondTag == 90))) {

if (tag1 == 90) {
    [self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed
    spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed

}
else if (tag2 == 90)   {
    [self destroyBodies:twoPhysicsSprite.b2Body];
    spriteToDestroy = twoPhysicsSprite;
}
}

The following method within HelloWorldLayer.mm is called in the update method to destroy the body and sprite with (tag == 90):

-(void)removeDestroyedBodiesAndSprites  {
if ([bodyContact arrayOfBodies]) {

for (NSValue* bodyValue in [bodyContact arrayOfBodies]) {
    b2Body *removeBody;
    removeBody = (b2Body*)[bodyValue pointerValue];

    world->DestroyBody(removeBody);
    removeBody = NULL;
    [self removeChild:[bodyContact spriteToDestroy]];

       }
    }
        CCLOG(@"y value for Sam sprite %f",[Sam samPhysicsSprite].position.y); // here is the CCLOG
}
4

0 回答 0