执行操作后执行操作?

时间:2015-06-24 21:31:57

标签: ios swift sprite-kit

我基本上有三个精灵。我想首先让它们全部淡出。一旦完成,我想对它们执行其他操作。这是我的代码:

    //Fade the sprites out
    oneMissedOrbIndicator.runAction(SKAction.fadeOutWithDuration(0.5))
    firstTwoMissedOrbsIndicator.runAction(SKAction.fadeOutWithDuration(0.5))
    secondTwoMissedOrbsIndicator.runAction(SKAction.fadeOutWithDuration(0.5))

    //Do this after all three sprites have been faded out
    switch numberOfCollectionOrbsNotCollected {
    case 0:
        break
    case 1:
        oneMissedOrbIndicator.runAction(SKAction.fadeInWithDuration(0.5))
    case 2:
        firstTwoMissedOrbsIndicator.runAction(SKAction.fadeInWithDuration(0.5))
        secondTwoMissedOrbsIndicator.runAction(SKAction.fadeInWithDuration(0.5))
    default:
        print("Unreachable switch statement case reached3!\n")
        exit(EXIT_FAILURE)

    }

然而,SpriteKit在进行下一个动作之前并没有等待淡出动作完成,所以基本上我想要发生的事情同时发生。

如何在所有三个精灵都淡出后执行switch语句?

我尝试了以下内容:

1)在淡化代码之后有一个while语句。 while循环保持循环,而#34; hasActions"任何精灵都是如此。这只是冻结了我的应用程序。

2)在"完成"中执行switch语句。关闭最后一个精灵我消失了。这与上面的代码完全相同 - switch语句由于某种原因同时发生。

2 个答案:

答案 0 :(得分:0)

创建一系列操作,如下所示:

user1   --  245 0   0
user2   --  245 102400  102400
user3   --  234 102400  102400
user4   --  234 1   0

答案 1 :(得分:0)

你可以检查完成这样的行动

var completed = false

let fadeOut = SKAction.fadeOutWithDuration(0.5)

 oneMissedOrbIndicator.runAction(fadeOut, completion: {self.completed = true})
    firstTwoMissedOrbsIndicator.runAction(fadeOut)
    secondTwoMissedOrbsIndicator.runAction(fadeOut)

由于您同时运行了这些操作,因此您只能检查一个操作的完成情况,以更改布尔值。您现在可以将switch语句放在update函数中以检查completed是否为true。

    override func update(currentTime: NSTimeInterval) {

if completed == true {

switch numberOfCollectionOrbsNotCollected {
    case 0:
        break
    case 1:
        oneMissedOrbIndicator.runAction(SKAction.fadeInWithDuration(0.5))
    case 2:
        firstTwoMissedOrbsIndicator.runAction(SKAction.fadeInWithDuration(0.5))
        secondTwoMissedOrbsIndicator.runAction(SKAction.fadeInWithDuration(0.5))
    default:
        print("Unreachable switch statement case reached3!\n")
        exit(EXIT_FAILURE)
}

}
相关问题