我是一名完整的编程初学者。在下面的代码中我想要做的是保存高分,我无法做到这一点。因为你我使用的是NSUserDefaults,但是一旦游戏结束,它就不会出现在屏幕上。任何帮助将不胜感激。我希望在屏幕上显示高分并且它没有。
类WT:SKScene {
let showMessage = SKLabelNode()
let tryAgain = SKLabelNode()
var highScore = SKLabelNode()
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor()
showMessage.fontName = "Noteworthy-Light"
showMessage.fontSize = 55
showMessage.fontColor = SKColor.redColor()
showMessage.text = "Time's Up!"
showMessage.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
showMessage.name = "show Message"
showMessage.hidden = false
showMessage.zPosition = 100
self.addChild(showMessage)
tryAgain.fontName = "Noteworthy-Light"
tryAgain.fontSize = 44
tryAgain.fontColor = SKColor.greenColor()
tryAgain.text = "Try Again!"
tryAgain.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 - 200)
tryAgain.name = "T.A"
self.addChild(tryAgain)
highScore = SKLabelNode(fontNamed: "Noteworthy-Light")
highScore.text = "0"
highScore.fontColor = SKColor.whiteColor()
highScore.fontSize = 35
highScore.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
highScore.name = "hs"
addChild(highScore)
var ActionOne = SKAction.scaleBy(0.9, duration: 0.3)
var ActionSequence = SKAction.sequence([ActionOne, ActionOne.reversedAction(), ActionOne, ActionOne.reversedAction()])
tryAgain.runAction(SKAction.repeatActionForever(ActionSequence))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if (node.name == "T.A"){
let myScene = Game(size: self.size)
myScene.scaleMode = scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)
}
}
func saveHighscore(highScore:Int){
if let currentHighscore:Int = NSUserDefaults.standardUserDefaults().valueForKey("highScore") as? Int{
if(highScore > currentHighscore){
NSUserDefaults.setValue(highScore, forKey: "highScore")
}
}else{
NSUserDefaults.setValue(highScore, forKey: "highScore")
}
}
}
答案 0 :(得分:1)
您可能希望在更改设置后同步设置,以便将修改写入磁盘。 Synchronize Apple
NSUserDefaults.standardUserDefaults().synchronize()