编辑:我发现我的游戏在第一次检索到高分时冻结了,并且每当新的高分存档时它就会冻结。是否有一种不太强烈的方式来做这个不会冻结我的游戏?感谢。
我一直在四处寻找游戏在第一次碰撞时可能会冻结的原因。当球与不会导致球员输球的物体碰撞时,游戏不会冻结,而是玩家第一次失败。我在网上找到的只是播放音频文件而没有正确预加载它可能是原因。为了验证我的音乐不是问题,我完全从游戏中删除了背景音乐并且问题仍然存在。下面是游戏冻结时调用的代码。同样,冻结仅在第一次调用此代码时发生。我很欣赏这方面的任何帮助。感谢。
gameOver功能:
func gameOver() {
state = .FSGameStateEnded
myScore.highScore = score
if retrievedHighScore.highScore < myScore.highScore
{
SaveHighScore().ArchiveHighScore(highScore: myScore)
}
retrievedHighScore = SaveHighScore().RetrieveHighScore() as! HighScore
label_highScore.text = "High Score: \(retrievedHighScore.highScore)"
label_finalScore.text = "Your Score: \(score)"}
}
以下是我检索和保存高分的代码: class HighScore:NSObject {
var highScore: Int = 0
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeInteger(highScore, forKey: "highScore")
}
init(coder aDecoder: NSCoder!) {
highScore = aDecoder.decodeIntegerForKey("highScore")
}
override init() {
}
}
类SaveHighScore:NSObject {
var documentDirectories:NSArray = []
var documentDirectory:String = ""
var path:String = ""
func ArchiveHighScore(#highScore: HighScore) {
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as! String
path = documentDirectory.stringByAppendingPathComponent("highScore.archive")
if NSKeyedArchiver.archiveRootObject(highScore, toFile: path) {
println("Success writing to file!\(highScore.highScore)")
} else {
println("Unable to write to file!")
}
}
func RetrieveHighScore() -> NSObject {
var dataToRetrieve = HighScore()
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as! String
path = documentDirectory.stringByAppendingPathComponent("highScore.archive")
if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? HighScore {
dataToRetrieve = dataToRetrieve2
}
return(dataToRetrieve)
}
}