0

我正在用 cocos2d 制作游戏,我有一堆想要删除的精灵。例如,我可能在屏幕上有一堆角色,但是当我的游戏结束时,我想清理它们。现在我已经创建了一个特殊效果(粒子系统)来分散注意力,但是因为它是透明的并且不会覆盖所有的屏幕,所以当我将它们从图层中移除时,你可以看到并看到精灵消失。

此外,由于指令对用户的执行速度如此之快,它看起来好像精灵在粒子效果开始之前就消失了!

对我的 2 个问题有什么建议吗?谢谢。

NSMutableArray *toRemove = [[NSMutableArray alloc] init]; // array of sprites that I collect to remove

spriteCount = 0;
    if([self findAllSprites:parent forRemoval:toRemove] > 0){ // is there is at least one sprite to delete. If not then don't do anything
        [self specialEffect]; // runs for maybe 3 seconds. 
                    // how can I stall here so that the sprites aren't removed "instantaneously"?
        for (Character* aCharacter in toRemove) {
            [aCharacter.parent remove:aCharacter];      
        }

}

4

2 回答 2

1

您需要在线程中执行“特殊效果”,以便它您的精灵删除一起运行。查找 NSThread 以获取更多信息,但这将使您能够同步两个进程。

于 2009-06-19T05:20:10.740 回答
1

您可以使用 performSelector:withObject:afterDelay: 延迟删除操作。例如:

    NSMutableArray *toRemove = [[NSMutableArray alloc] init]; // array of sprites that I collect to remove
    spriteCount = 0;
    if([self findAllSprites:parent forRemoval:toRemove] > 0){ // is there is at least one sprite to delete. If not then don't do anything
        [self specialEffect]; // runs for maybe 3 seconds. 
        [self performSelector:@selector(removeSprites:) withObject: toRemove afterDelay:1.0];
    }
    [toRemove release];

- (void) removeSprites: (NSArray*) toRemove
{
    for (Character* aCharacter in toRemove) {
        [aCharacter.parent remove:aCharacter];          
    }
}

请注意 performSelector:withObject:afterDelay: 将保留 toRemove 对象并使其保持活动状态,直到它调用 removeSprites,因此您不必对 toRemove 执行任何特殊操作(除非您仍然需要释放它,因为您拥有它以及)。

于 2009-06-19T06:19:33.077 回答