Reputation: 10930
Im getting an EXC BAD ACCESS error on a touch event. The line being highlighted is the:
if ([aCrate isKindOfClass:[Crate class]]) {
Im working in an ARC enabled project using cocos2d. I have no idea why this error is happening and could use some help debugging.
-(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;
}
}
}
}
}
Im adding the Crate object with this code and its not being removed anywhere
Crate* aCrate = [Crate createWithDictionary:crateDict];
[self addChild:aCrate z:zCrate];
Its driving me crazy, so any help would be great
Upvotes: 0
Views: 173
Reputation: 64477
I presume that when you call
[aCrate destroyed];
you're removing it from the children array by using removeChild or removeFromParentWithCleanup, is that correct?
If so this modifies the children array and this is illegal during enumeration. You will have to add the to-be-destroyed crate to a NSMutableArray and at the end of the method do:
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)];
Upvotes: 1