8

I saw that in XCode 6 there is a built in level editor. I could not find a lot of documentation on how to use it or how it works, but I was able to figure out how to use it to make SKSpriteNodes then link them to a .swift file using childNodeWithName(). When you make a new SpriteKit project, it starts with a GameScene.swift and a GameScene.sks. I was able to use these to make one level visually, then I could code for it in the GameScene.swift, but when I tried to make my own .sks and .swift file for a new level, I could not connect the .sks and the .swift file I had made. I tried following this tutorial exactly:

http://www.techotopia.com/index.php/An_iOS_8_Swift_Sprite_Kit_Level_Editor_Game_Tutorial#Creating_the_Archery_Scene

When I connected the two files no errors came up, and then when I transitioned to the scene it transitioned properly, but nothing that I added visually to the .sks showed up in the simulator. Please help, I've spent hours researching solutions for this. The very few tutorials and forums that I was able to find about the level editor only talked about the GameScene.swift and .sks that start with a new SpriteKit project.

Callum Ferguson
  • 190
  • 2
  • 12
  • can you show the code where you instantiate the new SKScene and transition to it? – rakeshbs Feb 24 '15 at 05:19
  • possible duplicate of [What's the relationship between GameScene.swift and GameScene.sks files in SpriteKit template](http://stackoverflow.com/questions/26765573/whats-the-relationship-between-gamescene-swift-and-gamescene-sks-files-in-sprit) – rickster Feb 26 '15 at 02:31

2 Answers2

5

This method will unarchive an sks file and initialise it as whichever SKNode subclass it is called on. This means you can unarchive a file as an SKNode (so the root node will be an SKNode) and add it as a child to your scene. You can also unarchive a file as GameScene or any SKNode subclass.

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

Use it like this:

let scene = GameScene.unarchiveFromFile("GameScene")! as GameScene

or

let levelStuff = SKNode.unarchiveFromFile("Level 1")!
self.addChild(levelStuff)
  • I'm sorry if my question had already been answered already. I did read that forum post that was linked by rickster, but it wasn't quite was I was looking for. I used the: let levelStuff = SKNode.unarchiveFromFile("Level 1")! self.addChild(levelStuff) to add the nodes from my .sks to my scene. I can now control visually added nodes with code. I plan to use this to just make static platforms at such, then use code to add my "hero". Thank you so much for helping me, it was a simple fix, and now I can easily make and design new levels for my game I've been making. – Callum Ferguson Feb 27 '15 at 04:12
  • 1
    Also, this is my first time EVER using any type of forum, so I'm sorry if I'm not doing things quite like they should be done – Callum Ferguson Feb 27 '15 at 04:18
  • If this answer solved your problem would you consider accepting it (click the green tick) –  Feb 27 '15 at 18:28
4

By the time of this answer (Xcode 8.2.1) the 'simple' way of doing this for scenes:

  • Select the .sks file in your Project Navigator so the Graphic Editor is displayed
  • Go to the Utilities Area (on the right hand side)
  • Click on Custom class tab (icon looks like a contact card)
  • In “Custom class” enter the name name of the class in your .swift file
Lennin
  • 481
  • 5
  • 11