Cocos2d:删除精灵时黑屏

时间:2013-03-07 20:27:26

标签: cocos2d-iphone

我目前正在做教程http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial。 当一个忍者之星击中怪物时,我遇到了一个问题。我的代码是:

- (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];

}

工作正常,不会崩溃,但是当我用弹丸击中怪物时,屏幕在模拟器上变黑。没有错误或任何事情。我记录了CGRectIntersectRect,它按照预期记录。问题是当发生这种情况时,它全都变黑了。知道为什么吗?

2 个答案:

答案 0 :(得分:2)

我查看了教程,我在上面的评论中确定的行是:

[self removeChild:monster cleanup:YES];

试试。

答案 1 :(得分:0)

您正在执行[self removeFromParentAndCleanup:YES],它会从父级中删除您当前的图层。所以你得到一个黑屏。

您可能希望从图层中删除子怪物。