我的高分数量不断上升和下降。 SwiftKit

我的高分数量不断上升和下降。 SwiftKit

问题描述:

我有一个spritekit游戏,我有一个高分。它使用NSUser默认。但是我得到了高分2,然后我完全关闭了应用程序,然后打开它显示我的高分2,然后得到一个作为分数。它仍然是2.但是,我再次关闭应用程序并打开它,它显示高分1.为什么这样做?这是我的代码。 if条件不起作用吗?注意:这只是缩小到高分代码。我的高分数量不断上升和下降。 SwiftKit

import SpriteKit 

//In the DidMoveToView function 
if let Highscore1 = defaults.stringForKey("Highscore"){ 
    HighScoreLabel.text = "HIGHSCORE: \(Highscore1)" 
} 
//In the touches began func 
//Making what happens when the User Fails and a new highscore is achieved 

if Score > highscore { 

    defaults.setObject("\(Score)", forKey: "Highscore") 

} 

预先感谢您

+0

我不记得你是否需要在iOS8中这样做,但以防万一,在同步被调用的任何地方?也许在setObject之后加上它来看看会发生什么。 –

的问题是你是从NSUserDefaults阅读高分,并显示在HighScoreLabel它。但是你没有在highscore可变分配/存储的值,因为它保持为0,即当您打开应用程序,使得下列条件为真,并播放了第一次:

if Score > highscore { 
    defaults.setObject("\(Score)", forKey: "Highscore") 
} 

您需要改变高分阅读部分,如:

if let Highscore1 = defaults.stringForKey("Highscore") { 
    HighScoreLabel.text = "HIGHSCORE: \(Highscore1)" 

    // Storing current high score to variable 
    highscore   = Int(Highscore1) 
}