简体   繁体   English

第二视图控制器中的高分和游戏统计

[英]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.

相关问题 在第二个 tableview 控制器上查看不同的统计信息 - Viewing different stats on 2nd tableview controller 委托无法在第二视图控制器中调用 - Delegate unable to call in 2nd view controller 在第二次尝试时查看控制器的数据更改 - View controller's data changes on 2nd attempt 将数据移动到另一个视图控制器,但是第二个视图控制器先加载第一个视图控制器 - move data to another view controller, but the 2nd view controller load first before 1st view controller ios从第二个视图控制器向第一个视图控制器发送Objective-c数据发送 - ios Delegate Objective-c data sending from 2nd view controller to 1st view controller 如何使用第一视图中的按钮更改第二视图 Controller 中的图像 Controller - How to Change an image in 2nd View Controller using a Button in 1st View Controller 根据“表格视图”选择为第二视图控制器中的IOS按钮分配功能 - Assigning function to IOS button in 2nd view controller based on Table View selection 将plist加载到tableview后,如何将信息传递给第二视图控制器? (NSDictionary) - After loading a plist into my tableview how do I pass the info to a 2nd view controller? (NSDictionary) 我只能使UINavigationController在第二级加载,而不能在Root View Controller加载 - I can make UINavigationController load only at 2nd level, not at Root View Controller 将DS设置为第二个视图时,一个视图控制器中的两个表视图和一个表视图不起作用 - Two Tableviews in One view controller and One tableview not working when setting DS for 2nd one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM