【问题标题】:swift-can't present my second scene from GameViewControllerswift-无法从 GameViewController 中呈现我的第二个场景
【发布时间】:2016-01-19 02:56:11
【问题描述】:

我有两个场景,GameScene 和 PlayScene。我可以展示我的 GameScene,但我不能展示 PlayScene,我使用的是相同的代码和正确的文件名。我使用 UIButton 来调用此函数。我什至试过不调用 showGameScene 只调用 goToPlayScene 还是不行。我正在使用 GameViewController 来呈现这两个场景。

func showGameScene()
{
    if let scene = GameScene(fileNamed:"GameScene") {
        // Configure the view.
        let skView = self.view as! SKView
        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true
        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}
func goToPlayScene()
{
    playButton.hidden = true
    Leaderboard.hidden = true

    if let scene = PlayScene(fileNamed:"PlayScene")
    {
        let skView = self.view as! SKView
        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true
        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }

}

【问题讨论】:

  • 您是否尝试在 skView.presentScene(scene) 处放置断点或在其后放置 print 语句?只是为了确保它实际上被调用
  • 是的,我尝试了打印,但没有调用该语句。如果我不使用 if 语句并强制扭曲它,它会给我 nil。
  • 等等,所以如果你只是在 skView.presentScene(scene) 之后放一个 print 语句并保持其他所有内容不变,那么 print 语句不会被调用?
  • 这表明您的程序没有评估条件 (scene = PlayScene(fileNamed:"PlayScene") 为真。请确保您有一个名为 PlayScene 的文件。
  • 好的,MailE 的回答应该会为您指明正确的方向

标签: ios iphone swift uiviewcontroller sprite-kit


【解决方案1】:

您是否创建了文件 PlayScene.sks?

【讨论】:

  • 好的,很好用! :) 在我使用 GameScene.swift 转换到 PlayScene.swift 之前它工作了,我猜它工作是因为它使用 GameScene.sks 作为 PlayScene.swift 对吗?
  • 我不确定。我刚刚在 Xcode 中创建了测试文件。
  • 是的,文件名正在寻找 sks 文件。因此,如果您执行PlayScene(fileNamed:"GameScene"),您将获得一个具有 GameScene sks 设计的 PlayScene 实例。如果你没有对 sks 文件做任何事情,那么它们甚至都不需要,你可以像初始化任何其他对象一样初始化它,例如PlayScene()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多