【问题标题】:SpriteKit - How to transition to a new scene with a timerSpriteKit - 如何使用计时器过渡到新场景
【发布时间】:2019-02-12 16:17:36
【问题描述】:

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

我的代码现在看起来像这样,基本上只是背景/徽标图像。

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

【问题讨论】:

  • 您是在构建一个实际的加载场景,还是只想显示您的徽标 5 秒?您可以使用 Launch.xib。如果该 xib 不够长,您可以使用主故事板添加一个处理您的徽标的视图,除非绝对必要,否则不需要精灵套件。

标签: swift timer sprite-kit transition scene


【解决方案1】:

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

例子:

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

【讨论】:

  • 我完全不推荐这个。如果您在加载场景中接到电话,您将遇到问题
  • 如果您需要在转换到 MainMenuScene 之前加载一些资源,那么这确实不是最好的选择。最好创建一个方法来加载资源并完成屏幕转换。但是如果你只需要显示一个标准的屏幕并按住 5 秒而不进行任何其他操作,那么调度器就绰绰有余了。
  • 调度器是不够的,因为如果在启动屏幕发生时再次来电,那么您的计时器可能会失火,或者您可能会以暂停状态呈现场景。一般的经验法则是永远不要在 Sprite Kit 中使用 Timer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多