gpt4 book ai didi

xcode - 如何添加硬币分数,以便玩家随着时间的推移积累越来越多的硬币,快速

转载 作者:行者123 更新时间:2023-11-30 10:17:54 24 4
gpt4 key购买 nike

我创建了一个分数和高分标签。每次玩家击中硬币,分数就会获得 1 品脱,每当分数高于高分时,高分就会更新。现在,除此之外我想更改乐谱的功能。我不希望每次游戏重新开始时都从 0 开始,我希望它从玩家在上一场比赛中赢得的最后金额开始。所以几乎只是一个分数标签,即使玩家关闭应用程序,它也会随着时间的推移不断积累越来越多的分数。

这是我当前的代码,每次玩家开始游戏时,正常分数从 0 开始,而每次分数达到高于高分时就会更新高分。

let highScoreLabel = SKLabelNode()

var score: Int {

switch (self) {

case coin: return 1

default: return 0

}
}

func playerScoreUpdate() {
playerScorelabel.text = "Score: \(playerScore)"
}

func saveHighScore(high:Int) {
NSUserDefaults.standardUserDefaults().setInteger(high, forKey: "highscore")
}
func highScore() -> Int {
return NSUserDefaults.standardUserDefaults().integerForKey("highscore")
}
func resetHighScore() {
NSUserDefaults.standardUserDefaults().removeObjectForKey("highscore")
}


func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}


if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(obstacleCategory)) != 0 {
ship.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)
}

if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
secondBody.node?.removeFromParent() // Changed line.
playerScore = playerScore + 1
playerScoreUpdate()
}

if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(diamondCategory)) != 0 {
secondBody.node?.removeFromParent() // Changed line.
}

//CHANGE TO YOU WON SCENE
//CHECK TO SEE IF COINS ARE 10, THEN YOU WON
if playerScore == 30 {

let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameWonScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)

}

if playerScore > highScore() {
saveHighScore(playerScore)
println("New Highscore = " + highScore().description)
highScoreLabel.text = "HighScore: \(highScore().description)"

} else {
println("HighScore = " + highScore().description ) // "HighScore = 100"

}
}

最佳答案

在您的 didMoveToView 方法中,您只需将 highscoreLabel 的值设置为 highScore() 方法的值即可:

playerScore = highScore()

然后您应该能够从最后一个高分开始。您不必更改其他内容,因为代码的其他部分应该很好地完成其职责。

关于xcode - 如何添加硬币分数,以便玩家随着时间的推移积累越来越多的硬币,快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29144406/

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