What is the method unarchiveFromFile in GameViewController for?

≡放荡痞女 提交于 2019-12-05 13:45:51

I haven't used Xcode 6 much, but here is my understanding (someone may correct me or expound) :

This is how your app utilizes the layout data etc for the GameScene (or any SKScene). If you click on the GameScene.sks file in the Project Navigator panel you get a visual editor for your GameScene.

If you want this layout data to be utilized, you would use that method. You can visually layout your GameScene in the scene editor without need to code locations, settings etc.

Following on from 0x141E's comment, you can change the unarchiveFromFile method to use generics so you can use it for different SKS or SKScene classes:

extension SKNode {
    class func unarchiveFromFile<T:SKScene>(file : NSString) -> T? {
        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 T
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

Just call it the same way but with the different scene content you want in there and it should work fine

if let scene = GameScene.unarchiveFromFile("Level1Scene") as? GameScene {
...

if let scene = GameScene.unarchiveFromFile("Level2Scene") as? GameScene {
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!