gpt4 book ai didi

ios - Swift iOS - 在 gameScene 中设置高分

转载 作者:行者123 更新时间:2023-11-29 01:46:42 25 4
gpt4 key购买 nike

我的第一个 iOS 游戏应用程序取得了很大进展,目前我正在研究高分元素,如果有人愿意帮助指导我如何使用正确的代码。

我已经学习了一些教程,但结果不一,到目前为止,我将每次屏幕按下时的分数增加 1(将更改但用于本文的目的),效果很好,但我需要一些帮助使用 NSUserDefaults 保存高分并显示高分。

下面是我的代码摘录,目前分数工作正常,但高分保持在 0 并且不会增加。任何帮助将不胜感激!

这是我的代码......

谢谢。

使用重新设计的代码进行编辑

// in gameScene:

var scoreLabelNode = SKLabelNode()
var score = NSInteger()
var highScoreLabelNode = SKLabelNode()
var highScore = NSInteger()

// in did move to view

score = 0
scoreLabelNode.fontName = "Helvetica-Bold"
scoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.8, y: self.frame.size.height / 1.2 )
scoreLabelNode.fontSize = 20
scoreLabelNode.alpha = 0.2
scoreLabelNode.zPosition = -30
scoreLabelNode.text = "Score \(score)"
self.addChild(scoreLabelNode)

// start of high score

var highScoreDefault = NSUserDefaults.standardUserDefaults()

if (highScoreDefault.valueForKey("highScore") != nil){
highScore = highScoreDefault.valueForKey("highscore") as! NSInteger!
highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
}

// highScore = 0
// if (score > highScore) {
// highScore = score
// highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
// }


highScoreLabelNode.fontName = "Helvetica-Bold"
highScoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.6, y: self.frame.size.height / 1.3 )

highScoreLabelNode.fontSize = 20
highScoreLabelNode.alpha = 0.2
highScoreLabelNode.zPosition = -30
highScoreLabelNode.text = "High Score \(score)"
self.addChild(highScoreLabelNode)

// in touches began

let incrementScore = SKAction.runBlock ({
++self.score
self.scoreLabelNode.text = "Score \(self.score)"
})

self.runAction(incrementScore)

// in update before each frame

// highScore = 0
if (score > highScore) {
highScore = score
highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
}

最佳答案

在我看来你从来没有打电话

 if (score > highScore) {
highScore = score
highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
}

再一次,你的高分是在哪里设置的?它是否在您提供的代码中未指出的地方被调用?

关于ios - Swift iOS - 在 gameScene 中设置高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31842698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com