从文件名加载SpriteKit级别 [英] Load SpriteKit levels from filename

查看:96
本文介绍了从文件名加载SpriteKit级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在创建的游戏,我希望能够创建许多可以轻松加载的自定义关卡.每个级别都应该有一个.sks接口文件和它自己的SKScene子类.swift文件.

For a game I'm creating, I want to be able to create a lot of custom levels that can be loaded easily. Each level should have a .sks interface file and its own SKScene subclass .swift file.

现在,它正在工作:

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

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

我在哪里创建这样的场景:

Where I create the scene like this:

if let scene = SKScene.unarchiveFromFile("Level1", theClass: Level1.classForKeyedUnarchiver())

但是,这不适用于大型馆藏,因为我无法遍历每个馆藏(例如它们的一个文件夹)中的每个馆藏.我需要这样的东西

But this doesn't work for large collections because I can't iterate over each level in, say, a folder of them. I need something like this

if let scene = SKScene.unarchiveFromFile("Level1", className:"Level1")

我尝试使用此答案从这样的字符串中获取类,但场景已加载好像它只是一个SKScene,而不是一个Level1对象.

I tried using this answer to get the class from the string like this, but the scene loaded as if it were just an SKScene, not a Level1 object.

let classFromString: AnyClass! = NSObject.swiftClassFromString("Level1")
    if let scene = SKScene.unarchiveFromFile("Level1", theClass: classFromString)

目前,我将使用此方法,但我认为这不是最好的方法:

For now, I'll work with this, but I don't think this is the best approach:

let levels = [
    ("Level1", Level1.classForKeyedUnarchiver()),
    ("Level2", Level2.classForKeyedUnarchiver())]

let level1 = levels[0]
let (fileName : String, theClass: AnyClass!) = classes[0]
if let scene = SKScene.unarchiveFromFile(fileName, theClass: theClass)

我只希望能够收集大量易于加载的SKScene子类,每个子类都有自己的接口文件.

I just want to be able to make a large collection of easily loadable SKScene subclasses each with their own interface file.

推荐答案

这可以使用NSClassFromString完成:

对于Swift 2:

extension SKScene {
    static func sceneWithClassNamed(className: String, fileNamed fileName: String) -> SKScene? {
        guard let SceneClass = NSClassFromString("Your_App_Name.\(className)") as? SKScene.Type,
              let scene = SceneClass.init(fileNamed: fileName) else { 
            return nil 
        }

        return scene
    }
}

或者Swift 1:

extension SKScene {
    static func sceneWithClassNamed(className: String, fileNamed fileName: String) -> SKScene? {
        if let SceneClass = NSClassFromString("Your_App_Name.\(className)") as? SKScene.Type,
           let scene = SceneClass(fileNamed: fileName) {
            return scene
        }

        return nil
    }
}

用法:

if let scene = SKScene.sceneWithClassNamed("MyScene", fileNamed: "MyScene") {
    view.presentScene(scene)
}

重要的是要注意,要使其正常工作,您的SKScene子类必须实现init(coder aDecoder: NSCoder),例如:

Its important to note that for this to work correctly your SKScene subclass must implement init(coder aDecoder: NSCoder), for example:

required init?(coder aDecoder: NSCoder) {
    // ...
    super.init(coder: aDecoder)
}

这篇关于从文件名加载SpriteKit级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆