我有一个联系人监听器来检测子弹和小行星之间的碰撞。在 beginContact 函数中,我检查精灵类型并从该函数中触发一个块。
该块在分配时由场景类设置,然后将联系人侦听器分配给 box2dworld。子弹和小行星都得到 box2d 实体和精灵。
问题是我可以访问精灵并对它们做任何我想做的事情(运行动作、停止动作等)。
但是,只要我在精灵上调用 removeChild 函数,我就会在侦听器->BeginContact(this) 上的 b2Contact::Update 中得到一个 EXC_BAD_ACCESS。
谁能指出我正确的方向?
我的刻度功能:
//Physics simulation
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
b2Vec2 b2Position = b2Vec2(sprite.position.x/PTM_RATIO,
sprite.position.y/PTM_RATIO);
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(b2Position, b2Angle);
}
}
for(CCSprite *sprite in spritesToDelete) {
[self removeSprite:sprite];
[spritesToDelete removeObject:sprite];
sprite = nil;
}
我的联系人监听回调块:
contactListener->contactBlock = ^(CCSprite *A, CCSprite *B) {
NSLog(@"A: %@", [A class]);
NSLog(@"B: %@", [B class]);
CCSprite *bullet = nil , *asteroid = nil;
if ([(NSString *)A.userData isEqualToString:@"asteroid"] && [(NSString *)B.userData isEqualToString:@"bullet"])
{
asteroid = A;
bullet = B;
} else if ([(NSString *)B.userData isEqualToString:@"asteroid"] && [(NSString *)A.userData isEqualToString:@"bullet"])
{
asteroid = B;
bullet = A;
}
if (asteroid != nil && bullet != nil) {
NSLog(@"Asteroid Hit!");
[asteroid stopAllActions];
[bullet stopAllActions];
//If I keep the line below uncommented, I get the error. Adding actions and stuff does not make an issue, only removal of sprite is when I get the error.
[spritesToDelete addObject:bullet];
}
};