在Swift 4中保存HighScore

时间:2018-10-03 22:02:28

标签: swift nsuserdefaults

此代码包含我试图使用的所有标签。我无法保存高分并将其实施到游戏中。现在俗话说,我需要打字,所以我将继续打字直到它告诉我我很好。它仍然没有告诉我我好,我实际上非常惊讶。

import UIKit
import CoreData
import SpriteKit


var timer:Timer?
var seconds:Int = 5
var maxSeconds: Int = 5
var totalPoints:Int = 0
var high:Int = 0






let userDefaults = UserDefaults.standard
let defaults = UserDefaults.standard


class ViewController: UIViewController {







@IBOutlet weak var menu: UIButton!

var i = 0
var point = 0

@IBOutlet weak var highScore: UILabel!

@IBOutlet weak var timeLabel:UILabel?

@IBOutlet weak var points:UILabel?

@IBOutlet weak var totalPoint: UILabel!



@objc func tapped(){
    i += 1
    switch i {
    case 1:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.error)

    case 2:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)

    case 3:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.warning)

    case 4:
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.impactOccurred()

    case 5:
        let generator = UIImpactFeedbackGenerator(style: .medium)
        generator.impactOccurred()

    case 6:
        let generator = UIImpactFeedbackGenerator(style: .heavy)
        generator.impactOccurred()

    default:
        let generator = UISelectionFeedbackGenerator()
        generator.selectionChanged()
        i = 0

    }
}



func updateTimeLabel()
{
    if(timeLabel != nil)
    {
        let sec:Int = seconds % 30

        let sec_p:String = String(format: "%02d", sec)

        timeLabel!.text = "\(sec_p)"
    }
}



@objc func onUpdateTimer() -> Void
{
    if(seconds > 0 && seconds <= maxSeconds)
    {
        seconds -= 1

        updateTimeLabel()
    }
    else if(seconds == 0)
    {
        if(timer != nil)
        {
            timer!.invalidate()
            timer = nil

            userDefaults.set(totalPoints, forKey: "totalPoints")




            let alertController = UIAlertController(title: "Time Up!", message: "Your time is up! You got a score of \(point) points and your total coins now is \(totalPoints). You Can Do Better", preferredStyle: .alert)

            let restartAction = UIAlertAction(title: "Play Again!", style: .default, handler: nil)
            alertController.addAction(restartAction)



            let FirstSubview = alertController.view.subviews.first
            let AlertContentView = FirstSubview?.subviews.first
            for subview in (AlertContentView?.subviews)! {
                subview.backgroundColor = UIColor(red: 226/255.0, green: 158/255.0, blue: 152/255.0, alpha: 5.0)
                subview.layer.cornerRadius = 1
                subview.alpha = 1
            }

            self.present(alertController, animated: true, completion: nil)

            point = 0
            seconds = maxSeconds

            updateTimeLabel()
            menu.isHidden = false

            defaults.set(high, forKey: "high")




            }

        }
    }







@IBAction func Restart(_ sender: Any) {


}



@IBAction func adder(_ sender: Any)
{

    point += 1

    points?.text = "\(point)"
    if point % 10 == 0 {
        totalPoints = 10 + totalPoints
        totalPoint?.text = String(totalPoints)
    }

    if(timer == nil)
    {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector:#selector(onUpdateTimer), userInfo:nil, repeats:true)
    }

    tapped()
    menu.isHidden = true

}







override func viewDidLoad() {

    points?.text = "\(point)"
    let total = userDefaults.integer(forKey: "totalPoints")
    if total != 0 {
        totalPoints = total
    } else {
        totalPoints = 0
    }

    let score = defaults.integer(forKey: "high")
    if high < point {
        high = score
    } else {
        high = 0
    }







    totalPoint?.text = String(totalPoints)



    updateTimeLabel()

   highScore.text = String(high)



        }
    }

我需要在结尾处放些东西吗?好吧,看来这也不起作用!

1 个答案:

答案 0 :(得分:0)

#for example


 func saveHighScore() {
UserDefaults.standard.set(score, forKey: "HIGHSCORE")
}