为什么这个bool值不能保存?

时间:2017-06-02 23:37:59

标签: swift3

我正在尝试使用NSUserdefaults保存一条简单的信息。我正在尝试使用bool值将SKSprite保存为alpha为1。我是这样做的。

第一个场景:等级选择(精灵alpha为0.2) 当用户完成Level :(在Level Select中编辑精灵等于1)

GameViewController:

//
//  GameViewController.swift
//  Maze
//
//  Created by PixelatedAngel on 4/24/17.
//  Copyright © 2017 PixelatedAngel. All rights reserved.
//

import UIKit
import SpriteKit
import GameplayKit

public let levelselect = SKScene(fileNamed: "LevelSelect")

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = levelselect {
            // 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 supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func viewWillAppear(_ animated: Bool) {
    //saved data here
    if let x = UserDefaults.standard.object(forKey: "LevelOne")
    {
        isLevelOneCompleted = x as! Bool
    }

    if let z = UserDefaults.standard.object(forKey: "LevelTwoButton")
    {
        unlockLevelTwoButton = z as! Bool
        print("Trying to Retrive the value of Level Two Button")
    }
}
}

第一级:

//
//  GameScene.swift
//  Maze
//
//  Created by PixelatedAngel on 4/24/17.
//  Copyright © 2017 PixelatedAngel. All rights reserved.
//

import SpriteKit
import GameplayKit
import CoreMotion

public var isLevelOneCompleted = false
public var unlockLevelTwoButton = false

class LevelOne: SKScene, SKPhysicsContactDelegate {
    var levelonescene:SKScene!
    var player = SKSpriteNode()
    var endNode = SKSpriteNode()
    let manager = CMMotionManager()

override func didMove(to view: SKView) {
    self.physicsBody = SKPhysicsBody (edgeLoopFrom: self.frame)
    self.physicsWorld.contactDelegate = self

    player = self.childNode(withName: "player") as! SKSpriteNode
    player.physicsBody?.categoryBitMask = 1
    endNode = self.childNode(withName: "endNode") as! SKSpriteNode
    endNode.physicsBody?.categoryBitMask = 2
    manager.startAccelerometerUpdates()
    manager.accelerometerUpdateInterval = 0.1
    manager.startAccelerometerUpdates(to: OperationQueue.main){
        (data, error) in

        self.physicsWorld.gravity = CGVector(dx: ((data?.acceleration.x)! * 10), dy: ((data?.acceleration.y)! * 10))
    }
}

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("Level One complete")
        levelOneCompleted()
        unlockLevelTwo()
        //3 stars
        threeStars?.scaleMode = .fill
        self.view?.presentScene(threeStars!, transition: .fade(withDuration: 0.3))
 }
        }

func levelOneCompleted(){
    isLevelOneCompleted = true
}

func unlockLevelTwo(){
    unlockLevelTwoButton = true
    print("unlock level two = true")
}        
}

三星:

//
//  GameScene.swift
//  Maze
//
//  Created by PixelatedAngel on 4/24/17.
//  Copyright © 2017 PixelatedAngel. All rights reserved.
//

import SpriteKit
import GameplayKit
import CoreMotion

public let levelselectscene = SKScene(fileNamed: "LevelSelect")

class LevelCompletedThreeStar: SKScene, SKPhysicsContactDelegate {

    override func didMove(to view: SKView) {
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if isLevelOneCompleted == true{
            print("islevelonecompleted is true..")
            UserDefaults.standard.set(isLevelOneCompleted, forKey: "LevelOne")
            UserDefaults.standard.set(unlockLevelTwoButton, forKey: "LevelTwoButton")
            UserDefaults.standard.synchronize()
            levelselect?.scaleMode = .fill
            levelselect?.childNode(withName: "levelTwoButton")?.alpha = 1
            self.view?.presentScene(levelselect)
        }
}
}

等级选择:

//
//  GameScene.swift
//  Maze
//
//  Created by PixelatedAngel on 4/24/17.
//  Copyright © 2017 PixelatedAngel. All rights reserved.
//

import SpriteKit
import GameplayKit

class LevelSelect: SKScene {

    override func didMove(to view: SKView) {

        if let z = UserDefaults.standard.object(forKey: "LevelTwoButton")
        {
            unlockLevelTwoButton = z as! Bool
            print("Trying to Retrive the value of Level Two Button in Level Select")
        }

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

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let levelOne = SKScene(fileNamed: "LevelOne")
        levelOne?.scaleMode = .fill

        let levelTwo = SKScene(fileNamed: "LevelTwo")
        levelTwo?.scaleMode = .fill

        for touch in touches {
            let location = touch.location(in: self)
            let node : SKNode = self.atPoint(location)
            if node.name == "levelOneButton" {
                self.view?.presentScene(levelOne!, transition: SKTransition.fade(withDuration: 0.2))
                }

            if isLevelOneCompleted == true && node.name == "levelTwoButton"{
                self.view?.presentScene(levelTwo!, transition: SKTransition.fade(withDuration: 0.2))

            }
        }
        }
    }

1 个答案:

答案 0 :(得分:1)

在视图控制器GameViewController中,您将获得已存储的值。稍后在应用程序生命周期中,您将实例化您的游戏场景:LevelOne。初始化LevelOne时,将全局变量设置为false [不检查用户默认值]。我想知道如果不是在另一个文件中声明你的全局,而是在视图控制器的顶部定义它,并在检查用户默认值后在viewDidLoad中设置它,那么它是否会有所作为。你甚至可以将全局变量设为静态。