2

I made a simple game where you have to dodge obstacles and collect coins. Each coin will give you 1 point. While playing the game there is a score label. How can I create a high score label that will remember the players high score even when they exit the game. I am also wondering how I can connect the high score with the game center.

Any help would be much appreciated.

So far this is how I determine when the winner has won a game.

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 {
        coin.removeFromParent()
        playerScore = playerScore + 1
        playerScoreUpdate()
    }

    //CHANGE TO YOU WON SCENE
    //CHECK TO SEE IF COINS ARE 10, THEN YOU WON
    if playerScore == 10 {

        let reveal = SKTransition.flipHorizontalWithDuration(0.5)
        let scene = GameWonScene(size: self.size)
        self.view?.presentScene(scene, transition: reveal)

    }



}

EDIT

saveHighScore(100)
var score = 99
if score > highScore() {
saveHighScore(score)
println("New Highscore = " + highScore().description)
} else {
println("HighScore = " + highScore().description )  // "HighScore =     100"
}
score = 127
if score > highScore() {
saveHighScore(score)
println("New Highscore = " + highScore().description)  // "New     Highscore = 127"
} else {
println("HighScore = " + highScore().description )
}

EDIT 2

func playerScoreUpdate() {
    playerScorelabel.text = "Score: \(playerScore)"
}

EDIT 3

func addHighScoreLabel() {

    // Player Score
    highScoreLabel.fontName = "DIN Condensed"
    highScoreLabel.fontSize = 28
    highScoreLabel.fontColor = SKColor.whiteColor()
    highScoreLabel.position = CGPoint(x: 500, y: size.height/1.09)
    highScoreLabel.text = "HighScore: \(highScore)"
    addChild(highScoreLabel)
}

?

Miles H.
  • 285
  • 2
  • 13
  • http://stackoverflow.com/a/27791418/2303865 – Leo Dabus Mar 07 '15 at 21:42
  • possible duplicate of [Saving And Loading Up A Highscore In Swift Using NSUserDefaults](http://stackoverflow.com/questions/27790382/saving-and-loading-up-a-highscore-in-swift-using-nsuserdefaults) – Leo Dabus Mar 07 '15 at 21:44
  • Thanks Leonardo. I just read the post. Unfortunately I can not comment on it so Im gonna have to ask you a question on this page. Where do you put the following code (See EDIT). – Miles H. Mar 07 '15 at 21:47
  • can you post your playerScoreUpdate() function – Leo Dabus Mar 07 '15 at 21:49

1 Answers1

2
var playerScore = 0

func playerScoreUpdate() {
    let highScore = NSUserDefaults().integerForKey("highscore")
    if playerScore > highScore {
         NSUserDefaults().setInteger(playerScore, forKey: "highscore")
    }
    playerScorelabel.text = "Score: \(playerScore)"
}

playerScore = 200
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") )  // 200

playerScore = 180
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") )  // 200


playerScore = 250
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") )  // 250


highScoreLabel.text = "HighScore: " + NSUserDefaults().integerForKey("highscore").description
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • playerScore = 200 has error 'Expected declaration' Where do you place that part of code, the part underneath the function? Shouldn't that be an if statement? – Miles H. Mar 07 '15 at 22:03
  • Can you please tell me how to fix that part? – Miles H. Mar 07 '15 at 22:11
  • This is a pseudo code. The if statement is inside the method. It only saves the playerScore if it is higher than the actual highscore – Leo Dabus Mar 07 '15 at 22:31
  • Leonardo! I think it's working. I looked at the other post of yours and got it to work. Now it only will print the high score, how can I get it in my actual screen. See EDIT 3 for what I have so far. Now it shows the last highscore in the game scene, but it does not update if I reach a higher game score. It still does show each new high score in the println. Any ideas? – Miles H. Mar 08 '15 at 19:27
  • highScoreLabel.text = "HighScore: " + NSUserDefaults.standardUserDefaults().integerForKey("highscore").description – Leo Dabus Mar 09 '15 at 00:12