3

I'm trying to do this for a while. I have a main scene for a game named PlayScene. I have a pause button there. When player is tapping that button I want to load another scene, named PauseScene. For visualy creating that scene I use Sprite kit level editor. So I have two files PauseScene.swiftand PauseScene.sks. But .sks content ( just a background for now) isn't unarchiving. I don't really know where could I make a mistake. So this is my transition via pause button, located in PlayScene

for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
            var scene = PauseScene(size: self.size)
            scene.paused = true
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            scene.scaleMode = .AspectFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        }
    }    

And this is what I have in PauseScene.swift:

class PauseScene: SKScene {
override func didMoveToView(view: SKView) {
    self.initPauseScene()
}
func initPauseScene() {

}

}

I tried this and it's not working very well. However that tutorial is closest I could get with my problem. It's not crushing and isn't showing any errors, but when I tap pause button it's transitioning to that scene without my background, just standard grey scene. This answer is not working for me. First suggestion show errors and when I tried second and tap the pause – game is crushing. I don't know, that level editor is made for easier work but for know it just doubles it for me. I even thought that problem is in my background image maybe. But its standard .png. Why my content isn't showing up? So I tried to copy GameViewController default code for loading GameScene and ended up with crashing. It's not the first time I have that error while trying to unarchive this. What does it mean? I got this when i tried to replace var scene = PauseScene(size: self.size) with a if let scene = PauseScene.unarchiveFromFile("PauseScene") as? PauseScene

Community
  • 1
  • 1
Burundanga
  • 668
  • 5
  • 15

1 Answers1

1

It may be transition to a grey scene due to spelling errors or the file not even being in the mainBundle. I have tested this and it works. You need to make sure that you are writing the exact name of the .sks file when loading or else you get a grey screen.

Example

If the file is called GameScreen.sks and the class is called GameScene then if you put the class name when loading the .sks file you will get a grey screen.

let doors = SKTransition.doorwayWithDuration(1.0)
let archeryScene = GameScene(fileNamed: "GameScreen")
self.view?.presentScene(archeryScene, transition: doors)

So if we look at the second line of code, We see that it is loading the .sks file with the name GameScreen opposed to the class name GameScene. We also abort using the file extension because the compiler know by default it is a .sks file we are searching for in the mainBundle.

If you do keep getting a grey screen, Then it may be an error in spelling or ing you GameViewController, In the SKNode Extension, Be sure to change

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 GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

to this..

 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 SKScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
   }
}

We just edited this line of code,

 let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene

to this

 let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene

This will allow us to load multiple .sks files without any errors or crashes.

Community
  • 1
  • 1
Jesse Onolemen
  • 1,277
  • 1
  • 15
  • 32
  • Thank you so much for your time and helpfulness! Unfortunately I got mad and deleted my .sks file and set up everything programmatically so I can't try it now :D But I'll definitely do this for my next scene today and will respond how it worked! – Burundanga Mar 22 '15 at 16:46
  • I think that I'm cursed because it's not working. I know it's my mistake somewhere. I just don't know where to look. And one more thing – let's say I position everything in `.sks` file, saved the project, locked node selection. Am I ready to go? Maybe I need to do something more to apply the changes that I made? – Burundanga Mar 30 '15 at 19:53