如果游戏继续运行,我的最佳得分值非常好,但如果游戏关闭并再次启动,则无法获得最佳得分。每次重新启动时它都会回零。
以下是我正在做的事情:
import SpriteKit
class EM: SKScene, SKPhysicsContactDelegate {
var bestScoreText = SKSpriteNode()
let bestScoreCategory: UInt32 = 1 << 4
var bestScoreLabelNode = SKLabelNode()
var bestScore = NSInteger()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var bestScore = 0
var bestScoreDefault = NSUserDefaults.standardUserDefaults()
bestScore = NSUserDefaults.standardUserDefaults().integerForKey("bestScore")
}
func didBeginContact(contact: SKPhysicsContact) {
if (score > bestScore) {
bestScore = score
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(bestScore, forKey: "bestScore")
defaults.synchronize()
}
} else {
......
}
AppDelegate.Swift:
class AppDelegate: UIResponder, UIApplicationDelegate {
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["bestScore" : 0]
defaults.registerDefaults(defaultValues)
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
答案 0 :(得分:0)
首先,不要使用可选值,分数应该永远为零
处理user defaults
的常用方法是注册键值对以获得可靠的默认值。
所以在AppDelegate中 - 尽快 - 写一下
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["bestScore" : 0]
defaults.registerDefaults(defaultValues)
在每次应用程序启动时都会调用此代码并不重要。
如果user defaults
数据库中存在任何密钥,则忽略默认值。
将变量bestScore
声明为非可选Int
,起始值为0.
由于Int
是默认类型,因此可以省略类型。
var bestScore = 0
从user defaults
使用
bestScore = NSUserDefaults.standardUserDefaults().integerForKey("bestScore")
不需要检查nil
,因为我们知道默认的非可选值为0
将高分写入user defaults
使用
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(bestScore, forKey: "bestScore")
defaults.synchronize()