使用 NSuserdefaults 在 Swift 和 Sprite Kit 中设置 HighScore [英] Setting Up HighScore in Swift and Sprite Kit using NSuserdefaults
问题描述
我正在构建一个 swift 游戏并且需要设置一个高分,我一直试图弄清楚如何做到这一点.所以请您告诉我如何搜索该库以查看是否有已经保存的高分,然后将其与用户的分数进行比较.我试过提出这个,但它不起作用.请帮助并尽可能清楚.给我示例代码将不胜感激.
I am building a swift game and a need to set up a highscore , i have been trying to figure out how to do it for a while. So please can you tell me how to search this library to see if there is a already saved high score and then compare it to the user's score. I have tried coming out with this but it won't work. Please Help and be as clear as possible. Giving me example code would be extremely appreciated.
if let highscore: AnyObject = NSUserDefaults.valueForKey("highscore") {
var savedScore: Int = NSUserDefaults.standardUserDefaults().objectForKey("highScore") as Int
if ( savedScore < Score) {
var highscore = Score
NSUserDefaults.standardUserDefaults().setObject(highscore, forKey:"highscore")
NSUserDefaults.standardUserDefaults().synchronize() }
}
else {
var highscore = Score
NSUserDefaults.standardUserDefaults().setObject(highscore, forKey:"highscore")
NSUserDefaults.standardUserDefaults().synchronize()
}
推荐答案
使用 Single View Application 模板在 Xcode 中创建一个新的 iOS 项目.然后,将 ViewController.swift
的内容替换为以下代码:
Create a new iOS project in Xcode by using the Single View Application template. Then, replace the content of ViewController.swift
with the following code:
import UIKit
class ViewController: UIViewController {
var generator = [6, 12, 8].generate()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
button.setTitle("Button", forState: .Normal)
button.addTarget(self, action: "newScore", forControlEvents: .TouchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
// Auto layout code using anchors (requires iOS9+)
let horizontalConstraint = button.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
let verticalConstraint = button.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
NSLayoutConstraint.activateConstraints([horizontalConstraint, verticalConstraint])
// Print NSUserDefaults's highscore value when app is launched
print("highscore:", NSUserDefaults.standardUserDefaults().integerForKey("highscore"))
}
func newScore() {
guard let score = generator.next() else { return }
print("current score:", score)
//Check if score is higher than NSUserDefaults stored value and change NSUserDefaults stored value if it's true
if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
NSUserDefaults.standardUserDefaults().synchronize()
}
print("highscore:", NSUserDefaults.standardUserDefaults().integerForKey("highscore"))
}
}
在您的 iPhone 或模拟器上构建并运行项目.Xcode 控制台将打印 0,这是 NSUserDefaults.standardUserDefaults().integerForKey("highscore")
的当前值.
现在,点击按钮.这会将 score
的值设置为 6
并且,因为 6 >0
, NSUserDefaults.standardUserDefaults().integerForKey("highscore")
将获得这个新值.
Now, click on the button. This will set the value of score
to 6
and, because 6 > 0
, NSUserDefaults.standardUserDefaults().integerForKey("highscore")
will get this new value.
再次点击按钮.这会将 score
的值设置为 12
.您将立即在控制台中看到 NSUserDefaults.standardUserDefaults().integerForKey("highscore")
获得了这个新值.
Click once again on the button. This will set the value of score
to 12
. You will immediately see in the console that NSUserDefaults.standardUserDefaults().integerForKey("highscore")
gets this new value.
现在,如果您通过单击按钮将 score
的值更改为 8
,您将看到 NSUserDefaults.standardUserDefaults().integerForKey("highscore")
仍然有 12
的值,因为 8 <12
.
Now, if you change the value of score
to 8
by clicking on the button, you will see that NSUserDefaults.standardUserDefaults().integerForKey("highscore")
still has a value of 12
because 8 < 12
.
如果你重建你的项目并重新启动它,你会看到 NSUserDefaults.standardUserDefaults().integerForKey("highscore")
是持久的(它的值仍然是 12 并且没有被重置).因此,NSUserDefaults
可以成为存储玩家最高分的完美工具.
If you rebuild your project and relaunch it, you will see that NSUserDefaults.standardUserDefaults().integerForKey("highscore")
is persistent (its value is still 12 and has not been reset). Therefore, NSUserDefaults
can be a perfect tool in order to store your players highest scores.
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
总是返回一个 Int
(如果你没有自己设置它,它会返回 0
,而不是 nil
).因此,您不需要/不能对其使用可选绑定.如果你真的想处理一个可选的值返回,你会更喜欢 NSUserDefaults.standardUserDefaults().objectForKey("highscore") as?Int
.
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
always return an Int
(if you haven't set it by yourself, it will return 0
, not nil
). Thus, you dont need to / can't use optional binding with it. If you really want to deal with an optional value return, you will prefer NSUserDefaults.standardUserDefaults().objectForKey("highscore") as? Int
.
这篇关于使用 NSuserdefaults 在 Swift 和 Sprite Kit 中设置 HighScore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!