我正在用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];
}
}
答案 0 :(得分:1)
您需要在一个线程中执行'特殊效果',以便它与一起运行您的精灵删除。查找NSThread以获取更多信息,但这将使您能够同步这两个进程。
答案 1 :(得分: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执行任何特殊操作(除了您仍然需要释放它,如图所示)你也拥有它。)