适用于ios7的SpriteKit GameScene兼容性

时间:2016-01-13 14:36:54

标签: ios ios7 sprite-kit

我的应用程序适用于iOS 8+,但iOS 7搞乱了所有坐标 什么是复制创建gameScene for ios 7的实现的正确方法

这是我如何做到的:

   if #available(iOS 8, *) {
    if let scene = GameScene(fileNamed:"GameScene") {
        self.skView = self.view as! SKView

        skView.ignoresSiblingOrder = true

        scene.scaleMode = .AspectFill

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "quitToLevel:", name: "quitToLevelID", object:nil)
        skView.presentScene(scene)
    }
     } else {
        //here starts the wrong part
        if let scene = GameScene(size: view.frame.size) as GameScene? {

            let skView = self.view as! SKView

            scene.scaleMode = .AspectFill

            NSNotificationCenter.defaultCenter().addObserver(self, selector: "quitToLevel:", name: "quitToLevelID", object:nil)
            skView.presentScene(scene)
        }
    }

2 个答案:

答案 0 :(得分:0)

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

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

    }
}

答案 1 :(得分:0)

不确定它是否有所不同,但你应该将场景大小基于skView.bounds.size上的else语句

所以改成它

 } else {
    //here starts the wrong part
    let skView = view as! SKView!
    let scene = GameScene(size: skView.bounds.size)

    scene.scaleMode = .AspectFill

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "quitToLevel:", name: "quitToLevelID", object:nil)

    skView.presentScene(scene)
}

并查看它现在是否正常工作。

不确定这是否适用于ios 7,但值得一试。 您是否需要您的应用程序来支持ios 7?只有10%的用户在运行该特定操作系统。