我正在尝试在我的Sprite Kit游戏中重置我的高分,但是遇到了一些麻烦。
现在,我可以将HUD中的标签重置为零,但是当游戏重新开始时,标签中的分数会跳回到之前的位置,因为它们存储在NSUserDefaults中。
我正在重置标签:
override func didMoveToView(view: SKView) {
lblCoins.text = "0"
lblScore.text = "0"
}
我正在尝试像这样重置NSUserDefaults:
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "highScore")
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
我已尝试在我的游戏中随处放置这段代码,但似乎无法重置分数。只有在我完全重置应用程序时,分数才会重置。
以下是我尝试过的一些功能:
override func didMoveToView(view: SKView) {
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "highScore")
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
}
在GameOver上:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
...
case is GameOver:
let newScene = GameScene(fileNamed:"GameScene")
newScene!.scaleMode = .AspectFill
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
self.view?.presentScene(newScene!, transition: reveal)
self.saveHighScore("com.prismstudios.jumpingcarl.leaderboard", score: GameState.sharedInstance.highScore)
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "highScore")
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
default:
break
}
}
这是我在GameScene中添加点的地方:
func didBeginContact(contact: SKPhysicsContact) {
GameState.sharedInstance.highScore += 10
lblScore.text = String(format: "%d", GameState.sharedInstance.highScore)
GameState.sharedInstance.coins += 1
lblCoins.text = String(format: "%d", GameState.sharedInstance.coins)
}
GameState类:
class GameState {
var score: Int
var highScore: Int
var coins: Int
init() {
// Init
score = 0
highScore = 0
coins = 0
// Load game state
let defaults = NSUserDefaults.standardUserDefaults()
highScore = defaults.integerForKey("highScore")
coins = defaults.integerForKey("coins")
}
func saveState() {
// Update highScore if the current score is greater
highScore = max(score, highScore)
// Store in user defaults
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highScore, forKey: "highScore")
defaults.setInteger(coins, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
}
class var sharedInstance: GameState {
struct Singleton {
static let instance = GameState()
}
return Singleton.instance
}
}
任何人都可以提供帮助吗?
答案 0 :(得分:0)
如果你想重置一切,试试这个:
let defaults = `NSUserDefaults`.standardUserDefaults()
let appDomain: NSString = NSBundle.mainBundle().bundleIdentifier!
defaults.removePersistentDomainForName(appDomain as String)
如果你只是想重置你的高分,你必须选择正确的“一”。如果你正在做这样的事情:
defaults.setValue(123, forKey: "Highscore")
defaults.synchronize()
然后
defaults.setValue(0, forKey: "Highscore")
defaults.synchronize()
你有类似数组的东西:
Highscore1 = defaults.objectForKey("Highscore")![0] as? Int //123
Highscore2 = defaults.objectForKey("Highscore")![1] as? Int //0
您只是添加更多高分...尝试这样的事情:
func resetHighscore(){
let defaults = `NSUserDefaults`.standardUserDefaults()
defaults.removeObjectForKey("Highscore")
defaults.setValue(0, forKey: "Highscore")
defaults.synchronize()
}
答案 1 :(得分:0)
好的,我明白了。
之后,我将分数发布到游戏中心,然后我添加GameState.sharedInstance.highScore = 0
:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
switch gameState.currentState {
...
case is GameOver:
let newScene = GameScene(fileNamed:"GameScene")
newScene!.scaleMode = .AspectFill
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
self.view?.presentScene(newScene!, transition: reveal)
// Save high score to Game Center
self.saveHighScore("com.prismstudios.jumpingcarl.leaderboard", score: GameState.sharedInstance.highScore)
// Reset score and coins collected back to zero
GameState.sharedInstance.highScore = 0
GameState.sharedInstance.coins = 0
default:
break
}
}
我不确定为什么它以这种方式工作而且NSUserDefaults失败了,但我知道它与我有一个GameState帮助类的事实有关。