gpt4 book ai didi

swift - 如何使用 UserDefaults 快速将高分保存到游戏中?

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:53 25 4
gpt4 key购买 nike

<分区>

到目前为止,这是我所做的,但我的高分没有改变。

我忽略了所有不相关的变量等

如果有人能帮助我,那就太好了。我一直在尝试这样做 2 天大声笑帮助

class GameScene: SKScene, SKPhysicsContactDelegate {

var score = Int()

let scoreLabel = SKLabelNode()

var highScoreLabel = SKLabelNode()

var highScore = UserDefaults().integer(forKey: "HIGHSCORE")


func createScene(){



self.physicsWorld.contactDelegate = self

scoreLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + self.frame.height / 3)
scoreLabel.text = "\(score)"
scoreLabel.zPosition = 5
scoreLabel.fontSize = 60
scoreLabel.fontColor = UIColor.white
self.addChild(scoreLabel)

highScoreLabel.text = "High Score = \(UserDefaults().integer(forKey: "HIGHSCORE"))"
highScoreLabel.fontColor = UIColor.white
highScoreLabel.position = CGPoint(x: 120, y: 20)
addChild(highScoreLabel)

highScoreLabel.zPosition = 6



func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == physicsCategory.score && secondBody.categoryBitMask == physicsCategory.man{


score += 1
scoreLabel.text = "\(score)"
firstBody.node?.removeFromParent()

if score < UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()
}


}
else if firstBody.categoryBitMask == physicsCategory.man && secondBody.categoryBitMask == physicsCategory.score{

score += 1
scoreLabel.text = "\(score)"
secondBody.node?.removeFromParent()

if score > UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()
}

}


func saveHighScore() {

UserDefaults.standard.integer(forKey: "HIGHSCORE")
highScoreLabel.text = "High Score = \(UserDefaults().integer(forKey: "HIGHSCORE"))"


}

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