2017-03-07 92 views
1

如何更改新xcode3中的初始場景?我發現很多的指令,但是這一切與此代碼的工作:更改spritekit swift中的初始場景3

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

}

但新的Xcode項目,GameViewController沒有包含這部分代碼,當我試圖複製,它不能正常工作。所以我製作了MainScene.swift和MainScene.sks,並且想把這些場景設置爲main。接下來是什麼?

謝謝!

回答

2

如果您的場景佈局使用.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 
    } 
}