[英]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.
现在,我已将游戏玩法提高到所需的水平,我想添加一个游戏统计信息页面,以补充Game Center的成就和排行榜。
At present my GameScene.swift
holds my highscore and displays on the correct view controller using this code 目前,我的
GameScene.swift
拥有我的高分,并使用此代码显示在正确的视图控制器上
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. 现在我想做的是在当前连接和设置的
statsViewController
上显示相同的高分。 Obviously I want to use NSUserDefaults
but it doesn't work just replicating the above code in the stats view controller. 显然,我想使用
NSUserDefaults
但仅在stats视图控制器中复制上面的代码是行不通的。
UPDATE TO INCLUDE CODE FROM StatsViewController
更新以包含来自
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.
希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.