I read the answer to a similar question here how save the current level But apparently the question was not resolved there. Who can tell me how can I save the current level of my game? Each level in the files Level-1.json, Level-2.json, Level-3.json and etc. I am beginner in coding. I hope for your help guys.
I'm trying to use that in my GameViewController.swift but current level still don't save. What am I doing wrong?
import UIKit
import SpriteKit
import GameplayKit
import AVFoundation
class GameViewController: UIViewController {
...
var currentLevelNum = 1
...
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.set(currentLevelNum, forKey: "LevelKey")
let lastSavedLevelNum = UserDefaults.standard.object(forKey: "LevelKey")
setupLevel(levelNum: lastSavedLevelNum as! Int)
backgroundMusic?.play()
}
func setupLevel(levelNum: Int) {
let skView = view as! SKView
skView.isMultipleTouchEnabled = false
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .aspectFill
level = Level(filename: "Level_\(levelNum)")
scene.level = level
scene.addTiles()
scene.swipeHandler = handleSwipe(_:)
skView.presentScene(scene)
beginGame()
}