Search code examples
iphonecocos2d-iphonespritecgrectbounding-box

Exception thrown when constructing CGRect


I am making a game in which the player goes up to the exit and is transported to the next level. When I compile the project, I get the message: EXC_BAD_ACCESS (code = 2, address = 0x791)

I use this code to set up the exit:

_exit = [CCSprite spriteWithSpriteFrameName:@"exit.png"];
    CGPoint exitTileCoord = ccp(908, 980);
    CGPoint exitTilePos = [self positionForTileCoord:exitTileCoord];
    _exit.position = exitTilePos;
    [_batchNode addChild:_exit];

and I check whether the player has hit the exit with this code:

CGRect tankBoundingBox = CGRectMake(_tank.position.x - _tank.contentSize.width/2, _tank.position.y + _tank.contentSize.height, _tank.contentSize.width, _tank.contentSize.height);
CGRect exitBoundingBox = CGRectMake(_exit.position.x - _exit.contentSize.width/2, _exit.position.y + _exit.contentSize.height, _exit.contentSize.width, _exit.contentSize.height);
for (Tank *enemy in _enemyTanks) {
                if (CGRectIntersectsRect(sprite.boundingBox, enemy.boundingBox)) {

                    [childrenToRemove addObject:sprite];
                    enemy.hp--;
                    if (enemy.hp <= 0) {
                        [_enemyTanks removeObject:enemy];
                        [childrenToRemove addObject:enemy];
                    } else {
                    }
                }
            }

I get the error at the line where I set up the exitBoundingBox, but not the tankBoundingBox. Can someone tell me what I am doing wrong?

If it's any help, I am using Ray Wenderlich's tutorial to do this, and I copied the code exactly how he had it on his website (although I added in the BoundingBoxes).


Solution

  • Based on comments for the OP:

    Not sure how ccp works, but it looks like one of the values you are using to construct the cgrect is dirty.