我创建了一个得分和高分标签。每次玩家击中硬币时得分为1品脱,每当得分高于高分时高分得到更新。现在,除此之外,我想改变乐谱的功能。每次游戏重启时我都不希望它从0开始,我希望它从上一场比赛中玩家赢得的最后金额开始。因此,即使玩家关闭应用程序,也只是一个分数标签,随着时间的推移不断积累越来越多的分数。
这是我目前的代码,正常分数从每次玩家开始游戏时的0开始,每当分数达到高于高分的高分时获得更新的高分。
let highScoreLabel = SKLabelNode()
var score: Int {
switch (self) {
case coin: return 1
default: return 0
}
}
func playerScoreUpdate() {
playerScorelabel.text = "Score: \(playerScore)"
}
func saveHighScore(high:Int) {
NSUserDefaults.standardUserDefaults().setInteger(high, forKey: "highscore")
}
func highScore() -> Int {
return NSUserDefaults.standardUserDefaults().integerForKey("highscore")
}
func resetHighScore() {
NSUserDefaults.standardUserDefaults().removeObjectForKey("highscore")
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(obstacleCategory)) != 0 {
ship.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)
}
if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
secondBody.node?.removeFromParent() // Changed line.
playerScore = playerScore + 1
playerScoreUpdate()
}
if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(diamondCategory)) != 0 {
secondBody.node?.removeFromParent() // Changed line.
}
//CHANGE TO YOU WON SCENE
//CHECK TO SEE IF COINS ARE 10, THEN YOU WON
if playerScore == 30 {
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameWonScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)
}
if playerScore > highScore() {
saveHighScore(playerScore)
println("New Highscore = " + highScore().description)
highScoreLabel.text = "HighScore: \(highScore().description)"
} else {
println("HighScore = " + highScore().description ) // "HighScore = 100"
}
}
答案 0 :(得分:1)
在didMoveToView
方法中,您只需将highscoreLabel的值设置为highScore()
方法的值:
playerScore = highScore()
然后你应该可以从最后一个highScore开始。你不应该改变别的东西,因为代码的其他部分应该很好地完成它们的部分。