Swift, SpriteKit: how to save the whole progress of a scene

為{幸葍}努か 提交于 2019-12-30 05:14:05

问题


I have build a swift game with a "GameViewController.swift"

import UIKit
import SpriteKit

class GameViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()


    if let scene = GameScene(fileNamed:"GameScene") {
        // Configure the view.
        let skView = self.view as! SKView

        /* Set the scale mode to scale to fit the window */


        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
        }
    }
}

and i have a scene called "GameScene"(it is the default game-file when you open swift and create a new swift game)

import UIKit
import SpriteKit

class GameScene: SKScene{

var building = SKSpriteNode()

override func didMoveToView(view: SKView) {

    /* Setup your scene here */

}

a lot of things going on there. (buildings can be placed and that trigger some actions and so on)
But all the progress is lost when you leave the app.
How to save the whole progress of this "GameScene" and reload it at the start?


回答1:


SKScene conforms to NSCoding, so you can use this to archive your scene. Here is a tutorial that may help. https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding

The basic idea is you take the instance of your scene, and you archive it with:

NSKeyedArchiver.archivedData(withRootObject: scene)

Then take this and save it to disk or somewhere else

You can then unarchive it later with

NSKeyedUnarchiver.unarchiveObject(with: scene) as! SKScene

Now when you make a custom scene class, you need to be sure to include any variables you create that you need to save. This is where that pesky required init(coder aDecoder:NSCoder) initializer comes in that people tend to not use.

You basically use this initializer to decode your custom variables, like this.

required init(coder aDecoder: NSCoder) {
    player = aDecoder.decodeObject(forKey: "player") as! String
}

To save it to the archive, you add an encode method

func encode(with aCoder: NSCoder) {
    aCoder.encode(player, forKey: "player")
}



回答2:


You can archive your game state in the applicationDidEnterBackground(_:) event. Subscribe through NotificationCenter to get notified.

class GameScene : SKScene {
    required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)

        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: .UIApplicationDidEnterBackground, object: nil)
    }

    func applicationDidEnterBackground() {
        // Save Scene
        let sceneData = NSKeyedArchiver.archivedData(withRootObject: self)
        UserDefaults.standard.set(sceneData, forKey: "currentScene")
    }

    class func loadScene() -> SKScene? {
        var scene: GameScene?

        if let savedSceneData = UserDefaults.standard.object(forKey: "currentScene") as? NSData,
           let savedScene = NSKeyedUnarchiver.unarchiveObject(with: savedSceneData as Data) as? GameScene {
            scene = savedScene
        } else {
            scene = nil
        }

        return scene
    }
}

Scene loading happens in viewDidLoad, by first attempting to locate a previously archived scene or creating a new scene if no archived scene exists.

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! SKView? {
            guard let scene = GameScene.loadScene() ?? SKScene(fileNamed: "GameScene") as? GameScene else {
                fatalError("Scene not loaded")
            }

            scene.scaleMode = .resizeFill

            view.presentScene(scene)            
            view.ignoresSiblingOrder = true
        }
    }
}

You'll also need to make game elements NSCoding compliant, as mentioned in other answers.



来源:https://stackoverflow.com/questions/46536497/swift-spritekit-how-to-save-the-whole-progress-of-a-scene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!