我在两个身体之间建立了一个旋转关节。当另一个身体与其中一个身体接触时(参见下面的Contacter.mm)(身体带有标签== 90),我用标签== 90摧毁身体。我放置一个CCLOG来跟踪旋转关节的精灵位置身体没有被摧毁。 CCLOG工作正常,直到它的联合体被摧毁。然后CCLOG停止打印到控制台。我怎么解决这个问题?以下是相关代码。
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;
}
}
在更新方法中调用HelloWorldLayer.mm中的以下方法来使用(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
}