简体   繁体   English

如何让Swift等待SKAction执行?

[英]How do you make Swift wait for SKAction to be executed?

I have the following Swift code, however, when I run it in Xcode Simulator, it skips straight to "I don't believe we have met before". 我有以下Swift代码,但是,当我在Xcode Simulator中运行它时,它会直接跳到“我不相信我们之前见过”。 How do I make Swift wait for "Welcome" to be executed first? 如何让Swift等待“ Welcome”首先执行?

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)



    }


}

Instead of calling startScreen.run() , call startScreen.run(_:completion) and do the things you want to do after the SKAction s have run inside the completion handler. 而不是调用startScreen.run() ,而是调用startScreen.run(_:completion)并在完成完成处理程序中运行SKAction之后执行您想做的事情。 See the documentation . 请参阅文档

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

You can use a timer which will wait for what you want to execute like this: 您可以使用一个计时器,它将等待您要执行的操作,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM