我在触摸事件中收到了EXC BAD ACCESS错误。突出显示的行是:
if([aCrate isKindOfClass:[Crate class]]){
我正在使用cocos2d在启用ARC的项目中工作。我不知道为什么会发生这种错误,并且可以使用一些帮助调试。
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[AppData sharedData]isGamePaused] == NO) {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for (Crate* aCrate in self.children) {
if ([aCrate isKindOfClass:[Crate class]]) {
if ([self collisionWithPoint:location andRadius:40 andPoint:aCrate.crateSprite.position andRadius:aCrate.crateSprite.contentSize.width/2] && [aCrate isDestroyed] == NO) {
CGPoint crateLocation = aCrate.crateSprite.position;
[self crateTouched:crateLocation];
ScoreNode* aScore = [ScoreNode createWithScore:[[objectDict objectForKey:@"CrateHit"]integerValue]];
aScore.position = aCrate.crateSprite.position;
[self addChild:aScore z:zScoreNode];
[aCrate destroyed];
score = score + 50;
}
}
}
}
}
我使用此代码添加Crate对象,而不是在任何地方删除
Crate* aCrate = [Crate createWithDictionary:crateDict];
[self addChild:aCrate z:zCrate];
它让我发疯,所以任何帮助都会很棒
答案 0 :(得分:1)
我猜你打电话时
[aCrate destroyed];
你是通过使用removeChild或removeFromParentWithCleanup从children数组中删除它,这是正确的吗?
如果是这样,这会修改children数组,这在枚举期间是非法的。您必须将待销毁的箱子添加到NSMutableArray并在方法的末尾执行:
NSMutableArray* toBeDestroyed = [NSMutableArray array];
for (Crate* aCrate in self.children) {
if ([aCrate isKindOfClass:[Crate class]]) {
...
if (needsToBeDestroyed) {
[toBeDestroyed addObject:aCrate];
}
}
}
// now you can destroy them
[toBeDestroyed makeObjectsPerformSelector:@selector(destroyed)];