1

我目前正在做教程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,它按原样记录。问题是,当这种情况发生时,一切都会变黑。知道为什么吗?

4

2 回答 2

2

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

[self removeChild:monster cleanup:YES];

试试看。

于 2013-03-07T20:53:10.570 回答
0

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

您可能想要从图层中移除子怪物。

于 2013-03-07T21:09:08.023 回答