Why is this not saving?

Viewed 39

I am trying to save a simple piece of information using NSUserdefaults. I am trying to save a SKSprite to have an alpha of 1. Here is how I am doing it.

First scene: Level select (sprite alpha is 0.2) When user completes Level: (edit sprite in Level Select to equal one)

GameViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = levelselectscene {
            // Set the scale mode to scale to fit the window

            scene.scaleMode = .fill



            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

override var shouldAutorotate: Bool {
    return true
}

Level Select:

 override func didMove(to view: SKView) {

    if unlockLevelTwoButton == true {
        levelselectscene?.childNode(withName: "LevelTwoButton")?.alpha = 1
           UserDefaults.standard.set(unlockLevelTwoButton, forKey: "LevelTwoUnlocked")
        print("I got this far")
    }

}

Level One:

func didBegin(_ contact: SKPhysicsContact) {

    var bodyA = contact.bodyA
    var bodyB = contact.bodyB
    let threeStars = SKScene(fileNamed: "LevelCompleted3Star")

    let fadeAction = SKAction.fadeAlpha(by: 1, duration: 0.45)






    if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 2 || bodyA.categoryBitMask == 2 && bodyB.categoryBitMask == 1{
        print("TEST")
        levelOneCompleted() //islevelonecompleted 
       unlockLevelTwoButton = true
        //3 stars
        threeStars?.scaleMode = .fill
        self.view?.presentScene(threeStars!, transition: .fade(withDuration: 0.3))



}

3 Stars:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {



    if isLevelOneCompleted == true{
        unlockLevelTwoButton = true
        UserDefaults.standard.set(isLevelOneCompleted, forKey: "LevelOne")

        UserDefaults.standard.synchronize()
        levelselectscene?.scaleMode = .fill
        levelselectscene?.childNode(withName: "levelTwoButton")?.alpha = 1
        self.view?.presentScene(levelselectscene)



    }

To me, it looks like the information should save. What am I doing wrong? I also have the keys set to retrieve:

 if let z = UserDefaults.standard.object(forKey: "LevelTwoButton")
    {
        unlockLevelTwoButton = z as! Bool
    }

Can't figure out why it's not saving!

1 Answers
Related