当我从GameScene切换到新场景时,我得到一个灰色屏幕,我的代码无法运行

时间:2015-06-07 02:20:25

标签: swift sprite-kit scene

出于某种原因,当我尝试从我的GameScene切换到第二个场景时,屏幕变为灰色,但我的第二个场景中写入的代码都没有被执行。我认为这个问题可能与GameViewController中的内容有关,因为当我将下面的代码从let scene = GameScene()切换到让scene = EndScene时,EndScene中的代码运行正常。我必须忽略一个简单的问题。如果我需要提供更多信息,请告诉我,我是spritekit的新手。

我目前有这段代码:

import UIKit
import SpriteKit

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = GameScene()
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .ResizeFill
        scene.anchorPoint = CGPointMake(0.5, 0.5)
        scene.size = skView.bounds.size 
        skView.presentScene(scene)
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> Int {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

我用来从GameScene切换到EndScene的代码是:

self.view?.presentScene(SKScene(fileNamed:"EndScene."))

1 个答案:

答案 0 :(得分:0)

你的SKScene构造函数正在寻找一个名为EndScene的文件,当它实际上是一个类时。尝试在GameViewController类中创建一个类似于viewDidLoad的函数:

func toEndScene() {
    let skView = self.view as! SKView
    let eScene = EndScene()
    eScene.scaleMode = .ResizeFill
    eScene.anchorPoint = CGPointMake(0.5, 0.5)
    eScene.size = skView.bounds.size 
    skView.presentScene(scene)
}

我认为这可行,因为在viewDidLoad中为你设置了为EndScene切换GameScene。