我的高分数量持续上升和下降。 Swift SpriteKit

时间:2016-04-14 17:42:31

标签: ios swift sprite-kit

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

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")

}

提前谢谢

1 个答案:

答案 0 :(得分:1)

问题是您正在阅读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)
}
相关问题