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. XCode http://i62.tinypic.com/14lhml2.png
Right now, this is working:
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")
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)
I just want to be able to make a large collection of easily loadable SKScene subclasses each with their own interface file.