按特定顺序执行一系列命令的最佳方法是什么?我一直对 ObjC 中该死的并发执行感到沮丧。这是其中一个案例,我意识到自己是一名设计师,而不是“真正的”编码员。
我正在 iOS 上试验 SpriteKit,我希望在能量计达到 <=0 时发生一系列事情。
- 调用一个方法在最终接触处制造爆炸。该方法接受有关爆炸位置和大小的参数。
- 之后调用另一个方法调用一个新场景,一个结果屏幕。
当我有机会看到最后一次爆炸之前调用新场景时,我的问题就出现了。
相关代码如下:
- (void) doGameOver
{
damageIndicator.progress = 0;
energyLeft.text = @"Energy:0%";
GameOver *newScene = [[GameOver alloc]initWithSize:self.size];
newScene.timeElapsed = [started timeIntervalSinceNow];
[self.view presentScene:newScene transition:[SKTransition fadeWithColor:[SKColor whiteColor] duration:1]];
[damageIndicator removeFromSuperview];
- (void) makeExplosionWithSize:(float)myBoomSize inPosition:(CGPoint)boomPosition
{
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"explosion" ofType:@"sks"];
SKEmitterNode *boom = [NSKeyedUnarchiver unarchiveObjectWithFile:myFile];
boom.position = boomPosition;
boom.particleSize = CGSizeMake(myBoomSize, myBoomSize);
[self addChild:boom];
[self runAction:self.playMySound];
- (void)adjustScoreWithDamage:(float)hitDamage atPosition:(CGPoint)pos
{
_damage = _damage -(hitDamage);
if (_damage < 0) {
//these are the two things I need to execute sequentially
[self makeExplosionWithSize:500 inPosition:pos];
[self doGameOver]
}
我尝试过使用 bool 值 (gameOver = YES) 的方案,但认为我可能需要创建一个完成处理程序,这让我头晕目眩。
谁能建议最简单的方法来完成这个?
提前谢谢你。
最佳答案
最简单的(不是最好的)可能是替换
[self doGameOver];
与
[self performSelector:@selector(doGameOver) withObject:nil afterDelay:2.0];
关于ios - 最佳实践 - ObjC 中命令的顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22076212/