0

I am making a game with 50+ levels. I can't seem to be able to save the game so that when the user later opens the game, it will be on the same level they the user left off at. I use the fallowing code to transition from level to level.

let leveltwo = levelTwo(fileNamed: "levelTwo")
leveltwo?.scaleMode = .aspectFill
self.view?.presentScene(leveltwo!, transition:SKTransition.fade(withDuration: 0.1)) 

I also have a "example.swift" and "example.sks" file for each level. My goal is to save the level that the user is on. Im not saving a high score, I know how to do that. But i'm not sure if its the same. Or how I can apply that it this.

1 Answers1

2

You may want to use UserDefaults.

So, for example, in your AppDelegate you would implement this:

func applicationDidEnterBackground(_ application: UIApplication) {
    UserDefaults.standard.set("level_file_path", forKey: "currentLevel")
}

func applicationDidBecomeActive(_ application: UIApplication) {
    let levelFilePath = UserDefaults.standard.string(forKey: "currentLevel")

    // load your file/level here
}
mugx
  • 9,869
  • 3
  • 43
  • 55
Horea
  • 201
  • 2
  • 14