简体   繁体   中英

Highscore & Game Stats in 2nd View Controller

I'm looking on building on my current release of my game 'Ginger Cat'. Now i've got the gameplay to the desired level I would like to add a game stats page that will compliment the Game Centre achievements and leaderboards.

At present my GameScene.swift holds my highscore and displays on the correct view controller using this code

    highScore = 0
    highScoreLabelNode.fontName = "Helvetica-Bold"
    highScoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.48, y: self.frame.size.height / 1.17 )
    highScoreLabelNode.fontSize = 30
    highScoreLabelNode.alpha = 0.7
    highScoreLabelNode.text = "Highscore \(score)"

    let highScoreDefault = NSUserDefaults.standardUserDefaults()
    if (highScoreDefault.valueForKey("Highscore") != nil){
        highScore = highScoreDefault.valueForKey("Highscore") as! NSInteger!
        highScoreLabelNode.text = NSString(format: "Highscore : %i", highScore) as String
    }
    self.addChild(highScoreLabelNode)

Now what I would like to do is display the same highscore on the statsViewController currently connected and set up. Obviously I want to use NSUserDefaults but it doesn't work just replicating the above code in the stats view controller.

UPDATE TO INCLUDE CODE FROM StatsViewController

let highscoreDefault = NSUserDefaults.standardUserDefaults()
highscoreDefault.setValue(highScore, forKey: "Highscore") //Expected Declaration error on this line
highscoreDefault.synchronize()

highScoreLabelNode.fontName = "Helvetica-Bold"
highScoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.48, y: self.frame.size.height / 1.17 )
highScoreLabelNode.fontSize = 30
highScoreLabelNode.alpha = 0.7
highScoreLabelNode.text = "Highscore \(score)"

self.addChild(highScoreLabelNode)

Would anyone be able to help here? Once I do the highscore I can work out the rest of the achievements myself.

Many Thanks.

I am guessing you wanted this on stats...

let highscoreDefault = NSUserDefaults.standardUserDefaults()
let score = highscoreDefault.valueForKey("HighScore")

highScoreLabelNode.fontName = "Helvetica-Bold"
highScoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.48, y: self.frame.size.height / 1.17 )
highScoreLabelNode.fontSize = 30
highScoreLabelNode.alpha = 0.7
highScoreLabelNode.text = "Highscore \(score)"


self.addChild(highScoreLabelNode)

You aren't trying to set the high score you are trying to get it. Hopefully that helps.

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