在spritekit swift 3中更改初始场景

时间:2023-01-22 21:20:56

how to change initial scene in new xcode3? I found lots of instructions, but all of it working with this code:

如何在新的xcode3中更改初始场景?我发现了很多说明,但所有这些都使用了这段代码:

extension SKNode {
  class func unarchiveFromFile(file : String) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! MenuScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}

}

}

But new Xcode project, GameViewController not contain this part of code and when I try to copy, it not work. So I make MainScene.swift and MainScene.sks and want to set these scene to main. What next?

但是新的Xcode项目,GameViewController不包含这部分代码,当我尝试复制时,它无法正常工作。所以我制作了MainScene.swift和MainScene.sks,并希望将这些场景设置为main。接下来是什么?

Thanks!!!!

谢谢!!!!

1 个解决方案

#1


2  

If you are using an .sks file for your scene layouts use the following code.

如果您使用.sks文件进行场景布局,请使用以下代码。

override func viewDidLoad() {

    super.viewDidLoad()

    if let view = self.view as! SKView? {

        // Load the SKScene from 'MainScene.sks'
        if let mainScene = MainScene(fileNamed: "MainScene")  {

            // Set the scale mode to scale to fit the window
            mainScene.scaleMode = .aspectFill
            // Present the scene
            view.presentScene(mainScene)
        }

        view.showsFPS = true
        view.showsDrawCount = true
        view.showsNodeCount = true
        view.showsPhysics = true
        view.showsDrawCount = true
    }
}

#1


2  

If you are using an .sks file for your scene layouts use the following code.

如果您使用.sks文件进行场景布局,请使用以下代码。

override func viewDidLoad() {

    super.viewDidLoad()

    if let view = self.view as! SKView? {

        // Load the SKScene from 'MainScene.sks'
        if let mainScene = MainScene(fileNamed: "MainScene")  {

            // Set the scale mode to scale to fit the window
            mainScene.scaleMode = .aspectFill
            // Present the scene
            view.presentScene(mainScene)
        }

        view.showsFPS = true
        view.showsDrawCount = true
        view.showsNodeCount = true
        view.showsPhysics = true
        view.showsDrawCount = true
    }
}