我工作的一个游戏,并且有一定的问题永远保存最好的成绩吧。 我使用CoreData现在,但我每次运行游戏时,将比分以INT(17)比较和显示的较高者。 对于这些,17号可能是第一个得分我,当我跑的比赛。 即最高分数不会被保存即可。
该代码看起来是这样的。 谢谢!
// Decide if the game ends
var actionArray: NSMutableArray = NSMutableArray()
actionArray.addObject(SKAction.runBlock({
var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
//Use CoreData
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName:"Score")
request.returnsObjectsAsFaults=false
request.predicate = NSPredicate(format:"fetchScore = %@","fetchScore")
var results:NSArray = context.executeFetchRequest(request, error: nil)
if (results.count > 0) {
var compareBest:Int = 0
var res = results[0] as NSManagedObject
compareBest = res.valueForKey("bestScore") as Int
if compareBest < self.score {
var newBestScore = NSEntityDescription.insertNewObjectForEntityForName("Score", inManagedObjectContext: context) as NSManagedObject
newBestScore.setValue("fetchScore", forKey:"fetchScore")
newBestScore.setValue(Int(self.score), forKey: "bestScore")
context.save(nil)
self.bestScore = self.score
}else{
self.bestScore = compareBest
}
}else{
var newBestScore = NSEntityDescription.insertNewObjectForEntityForName("Score", inManagedObjectContext: context) as NSManagedObject
newBestScore.setValue(Int(self.score), forKey: "bestScore")
newBestScore.setValue("fetchScore", forKey:"fetchScore")
context.save(nil)
self.bestScore = self.score
}
var gameOverScene:SKScene = GameOverScene(size: self.size, won: false, scored: self.score, best: self.bestScore)
self.view.presentScene(gameOverScene,transition:transition)
}))