Sophia De Abbaneio
Sophia De Abbaneio

Reputation: 63

Cocos2d: Black screen when removing sprites

I'm currently doing the tutorial http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial . I have a problem on the part that reacts to when a ninja star hits the monsters. My code is:

- (void)update:(ccTime)dt {    
    NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *projectile in _projectiles) {

        NSMutableArray *monstersToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *monster in _monsters) {
            if (CGRectIntersectsRect(projectile.boundingBox, monster.boundingBox)) {
                [monstersToDelete addObject:monster];
            }
        }

        for (CCSprite *monster in monstersToDelete) {
            [_monsters removeObject:monster];
            [self removeFromParentAndCleanup:YES];
        }

        if (monstersToDelete.count > 0) {
            [projectilesToDelete addObject:projectile];
        }
        [monstersToDelete release];
    }

    for (CCSprite *projectile in projectilesToDelete) {
        [_projectiles removeObject:projectile];
        [self removeChild:projectile cleanup:YES];
    }
    [projectilesToDelete release];

}

which works okay, does not crash, but when I hit a monster with an projectile, the screen turns black on the simulator. No error or anything. I logged the CGRectIntersectRect, and it records as it is supposed to. The problem is that when this happens, it all turns black. Any idea why?

Upvotes: 1

Views: 169

Answers (2)

Stephane Delcroix
Stephane Delcroix

Reputation: 16222

You're doing [self removeFromParentAndCleanup:YES] which removes your current layer from the parent. So you get a black screen.

You probably want to remove the child monster from the layer instead.

Upvotes: 0

YvesLeBorg
YvesLeBorg

Reputation: 9079

I looked at the tutorial, and the line i identified in the comments above reads :

[self removeChild:monster cleanup:YES];

Try that.

Upvotes: 2

Related Questions