0

在触摸事件中,我正在获取EXC BAD ACCESS错误。被突出显示的行是:触摸事件中的EXC BAD ACCESS错误

如果([aCrate isKindOfClass:[木箱类]]){

im使用的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* aCrate = [Crate createWithDictionary:crateDict]; 
[self addChild:aCrate z:zCrate]; 

它快把我逼疯了,所以任何帮助将是巨大的

回答

1

我相信当你致电

[aCrate destroyed]; 

你正在通过使用removeChild或removeFromParentWithCleanup从儿童数组中删除它,是否正确?

如果是这样会修改子数组,这在枚举过程中是非法的。你将不得不添加待销毁的箱子到一个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)];