我使用NSUserDefaults
设置了高分,如果对象与某个块接触,则游戏结束。如果分数高于高分,则使用新值更新高分。
如何使用按钮将高分重置为零?
func didBeginContact(contact: SKPhysicsContact) {
if moving.speed > 0 {
if ( contact.bodyA.categoryBitMask & scoreCategory ) == scoreCategory || ( contact.bodyB.categoryBitMask & scoreCategory ) == scoreCategory {
// Balloon has contact with score entity
score++
scoreLabelNode.text = String(score)
// Add a little visual feedback for the score increment
scoreLabelNode.runAction(SKAction.sequence([SKAction.scaleTo(1.5, duration:NSTimeInterval(0.1)), SKAction.scaleTo(1.0, duration:NSTimeInterval(0.1))]))
} else {
moving.speed = 0
balloon.physicsBody?.collisionBitMask = blockCategory
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
//Check if score is higher than NSUserDefaults stored value and change NSUserDefaults stored value if it's true
if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore")
{
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
NSUserDefaults.standardUserDefaults().synchronize()
}
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
var highscoreShow = defaults.integerForKey("highscore")
highscoreLabelNode = SKLabelNode(fontNamed: "MarkerFelt-Wide")
highscoreLabelNode.position = CGPointMake(CGRectGetMidX(self.frame), 530)
highscoreLabelNode.zPosition = 10
highscoreLabelNode.fontSize = 18
highscoreLabelNode.text = "Highscore: \(highscoreShow)"
self.addChild(highscoreLabelNode)
gameoverLabelNode = SKLabelNode(fontNamed: "MarkerFelt-Wide")
gameoverLabelNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
gameoverLabelNode.zPosition = 10
gameoverLabelNode.text = "GameOver"
self.addChild(gameoverLabelNode)
returnBtn.position = CGPointMake(CGRectGetMidX(self.frame), 250)
returnBtn.zPosition = 5
returnBtn.setScale(0.5)
self.addChild(returnBtn)
println("end the game")
self.canRestart = true
}
}
}
答案 0 :(得分:4)
要将高分重置为零,您需要使用的是:
//Sets the integer value for the key "highscore" to be equal to 0
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "highscore")
//Synchronizes the NSUserDefaults
NSUserDefaults.standardUserDefaults().synchronize()