你如何让Swift等待执行SKAction?

时间:2017-07-27 12:56:54

标签: ios swift swift3 sprite-kit

我有以下Swift代码,但是,当我在Xcode Simulator中运行它时,它会直接跳到"我不相信我们之前遇到过#34;。我如何让Swift等待#34;欢迎"先执行?

import SpriteKit
import GameplayKit

class GameScene: SKScene {

override func didMove(to view: SKView) {

    // Get label node from scene and store it for use later

    let animateList = SKAction.sequence([SKAction.fadeIn(withDuration: 1.0), SKAction.wait(forDuration: 2.0), SKAction.fadeOut(withDuration: 1.0)])

    let startScreen = SKLabelNode(fontNamed: "Helvetica Neue UltraLight")
    startScreen.text = "Welcome"
    startScreen.fontSize = 100.0
    startScreen.fontColor = SKColor.white
    startScreen.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(startScreen)
    startScreen.alpha = 0.0

    startScreen.run(animateList)

    startScreen.text = "I don't belive we have met before"

    startScreen.run(animateList)



    }


}

2 个答案:

答案 0 :(得分:3)

调用startScreen.run()而不是调用startScreen.run(_:completion),并在完成处理程序内运行SKAction之后执行您想要执行的操作。请参阅documentation

startScreen.run(animateList, completion: {
    self.startScreen.text = "I don't belive we have met before"
    self.startScreen.run(animateList)
})

答案 1 :(得分:0)

你可以使用一个等待你想要执行的计时器,如下所示:

 _ = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { _ in
   // do whatever you want after 1 second.
    }