【发布时间】:2015-03-19 21:04:42
【问题描述】:
我创建了一个分数和高分标签。玩家每次击中硬币时得分为 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"
}
}
【问题讨论】: