简体   繁体   中英

How to reset high score for NSUserDefaults in SpriteKit with Swift

I have set a high score using NSUserDefaults where if the object makes contact with a block, the game ends. If the score is greater than the high score, it updates the high score with the new value.

How would I reset the high score back to zero with a button?

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

        }
    }
}

To reset the high score back to zero all you need to use is:

 //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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM