Scene created in Sprite kit level editor is not working

后端 未结 1 1692
北恋
北恋 2021-01-06 14:21

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 lo

相关标签:
1条回答
  • 2021-01-06 15:12

    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.

    0 讨论(0)
提交回复
热议问题