isaa6 isaa6 - 1 year ago 78
iOS Question

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". How do I make Swift wait for "Welcome" to be executed first?

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)
startScreen.alpha = 0.0

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



Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download