-4

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()
    }
Sheron Li
  • 77
  • 1
  • 6
  • you can store the current level(like 2) in user default. For write & read value check this answer https://stackoverflow.com/a/31203348/4831567 – Pramod Tapaniya Oct 13 '18 at 10:33
  • @PramodTapaniya thank you deeply! I updated my question. Level still doesn't save. I am really new in Swift. Can you tell me where am I wrong? – Sheron Li Oct 14 '18 at 08:27

1 Answers1

1

what I can read from your code is that, you are saving the levelKey to the UserDefaults when the apps load the view with

UserDefaults.standard.set(currentLevelNum, forKey: "LevelKey")

Then you retrieve the the levelKey on the

UserDefaults.standard.object(forKey: "LevelKey")

but where do you save the retrieved number? maybe you can do this:

let lastSavedLevelNum = UserDefaults.standard.object(forKey: "LevelKey")

then

setupLevel(levelNum: lastSavedLevelNum)
Darwin Harianto
  • 435
  • 4
  • 15
  • Thank you very much! I switched to that code but still can't save. I added some code above. Maybe problem there? – Sheron Li Oct 15 '18 at 12:34
  • maybe you want to save save the current level at the end, when viewDidDisappear method get called. Your current code will always save level 1 in the userDefaults settings. If you want to save the current level, you need to rewrite the userDefaults value. let say, maybe when the user enter a new level, you can rewrite {UserDefaults.standard.set(currentUserLevel, forKey: "LevelKey")} – Darwin Harianto Oct 16 '18 at 08:10
  • thank you. but I am beginner. I don't understand how. anyway thank you – Sheron Li Oct 16 '18 at 13:00
  • you can try to move this code from the viewDidLoad method UserDefaults.standard.set(currentLevelNum, forKey: "LevelKey") put the code when the user entered a level, save the level at UserDefaults. Then when you try to start the game over again, you can just take the new UserDefaults value, without overwriting the value – Darwin Harianto Oct 17 '18 at 04:11
  • only `UserDefaults.standard.set(currentLevelNum, forKey: "LevelKey")` ? – Sheron Li Oct 17 '18 at 23:53
  • yes, that line will save the level at the user's database. at the viewDidLoad, you can try to get the saved level. Only put 'set' every time the user entered a new level, and erase the set at the viewDidLoad, so it won't overwrites your saved value. at the viewDidLoad method, maybe you want to put a 'guard var' function to prevent error if the user doesn't have a default value at the start – Darwin Harianto Oct 18 '18 at 01:00