I am currently trying to build a loading scene into my SpriteKit game with my developer logo on it and need to transition to the MainMenuScene after say 5 seconds. How would I go about that.
My code right now looks like this, which is basically just the background/logo image.
import SpriteKit
class LoadingScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "fatscoprion")
background.position = CGPoint (x: self.size.width / 2, y: self.size.height / 2)
background.zPosition = -1
self.addChild(background)
}
}
You can create a Scheduled Timer and configure a function to call your method that's creates and present the new scene.
Example:
class LoadingScene: SKScene {
var timer = Timer()
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "")
background.position = CGPoint (x: self.size.width / 2, y: self.size.height / 2)
background.zPosition = -1
self.addChild(background)
//Create a Scheduled timer thats will fire a function after the timeInterval
timer = Timer.scheduledTimer(timeInterval: 5.0,
target: self,
selector: #selector(presentNewScene),
userInfo: nil, repeats: false)
}
@objc func presentNewScene() {
//Configure the new scene to be presented and then present.
let newScene = SKScene(size: .zero)
view?.presentScene(newScene)
}
deinit {
//Stops the timer.
timer.invalidate()
}
}