SpriteKit - 如何使用计时器过渡到新场景

SpriteKit - How to transition to a new scene with a timer

我目前正在尝试在我的 SpriteKit 游戏中构建一个带有我的开发者徽标的加载场景,并且需要在 5 秒后转换到 MainMenuScene。我该怎么做。

我的代码现在看起来像这样,基本上就是 background/logo 图片。

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)
   }
}

您可以创建一个预定计时器并配置一个函数来调用您创建和呈现新场景的方法。

示例:

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()
    }
}