0

My game keeps crashing around this particular block of code.

The error message is Thread1: EXC_Bad_ACCESS(code =1) and the highlighted code is the following:

-(void)updateForArrays:(ccTime)delta
{
   for (CCSprite *child in [self children]){
        if (child.tag==2) {

            if (CGRectIntersectsRect(child.boundingBox, _ship.boundingBox)) {
                [self removeChild:child cleanup:YES];
                _score += 1;
                [_scoreLabel setString:[NSString stringWithFormat:@"Score : %d",_score]];
            }
        }if (child.tag ==3){
            if (CGRectIntersectsRect(child.boundingBox, _ship.boundingBox)) {
                CCScene *gameOverScene = [GameOverLayer gameOverScene];
                [[CCDirector sharedDirector] replaceScene:gameOverScene];

            }
        }
     }
}
0

2 Answers 2

3

You shouldn't modify ([self removeChild:child cleanup:YES]) collections (the [self children] array) while iterating. One way to go around this is to add objects for removal in a separate array and remove them after you're done checking for collisions.

Edit:

NSMutableArray *cleanupArray = [NSMutableArray array];
for (CCSprite *child in [self children]) {
    // ...
    [cleanupArray addObject:child]; // instead of [self removeChild:child cleanup:YES];
    // ...
}

// actual removal of children
for (CCSprite *child in cleanupArray) {
     [self removeChild:child cleanup:YES];
}
[cleanupArray removeAllObjects];
4
  • Let me make sure I understand your suggestion, are you saying to replace the [self removeChild:child cleanup:YES]; and instead have a separate array do the removing?
    – TheM00s3
    Commented Apr 15, 2014 at 18:48
  • Yes, after you're done with the enumeration. I've added an example to my post.
    – Alexander
    Commented Apr 15, 2014 at 18:53
  • This method solves the crash problem but creates a second problem. When an object now collides with the ship it does not always remove it or even register the collision. Initially the method above was written to solve that problem, but now that problem is back. Here is something else that is interesting. THe objects that are collided with but not removed give 10 points instead of 1 point as they should. This is quiet an interesting bug.
    – TheM00s3
    Commented Apr 15, 2014 at 20:01
  • Fixed the problem by moving the cleanup array method behind the last if.
    – TheM00s3
    Commented Apr 15, 2014 at 22:52
0

If all the children of self is of type CCSprite then your code will work. If not then you will face a crash. Because there is a chance that you might be forcefully typecasting a child which is of no CCSprite class. See if this code helps

-(void)updateForArrays:(ccTime)delta
{
for (id item in [self children]){
    if((item isKindOfClass:(CCSprite class)])
    {
     CCSprite *child = (CCSprite *)item;
      if (child.tag==2) {

        if (CGRectIntersectsRect(child.boundingBox, _ship.boundingBox)) {
            [self removeChild:child cleanup:YES];
            _score += 1;
            [_scoreLabel setString:[NSString stringWithFormat:@"Score : %d",_score]];
        }
    }if (child.tag ==3){
        if (CGRectIntersectsRect(child.boundingBox, _ship.boundingBox)) {
            CCScene *gameOverScene = [GameOverLayer gameOverScene];
            [[CCDirector sharedDirector] replaceScene:gameOverScene];

        }
    }
   }
 }
 }
3
  • That should still work unless sending a message that is not in the CCNode baseclass. Both tag and boundingBox are declared in CCNode. It's bad practice nevertheless.
    – CodeSmile
    Commented Apr 15, 2014 at 19:30
  • @learnCocos2d- we never know if during game development time there comes a requirement of adding a non ccnode base object. This way the code will be in safe side that we are not typecasting forcefully and end up with a crash. Commented Apr 15, 2014 at 20:00
  • This method doesn't work since child.tag is no longer recognized and gives an undeclared variable error, and the crashing problem isnt solved.
    – TheM00s3
    Commented Apr 15, 2014 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.