gpt4 book ai didi

xcode - 如何使用sprite kit和swift在游戏中设置高分

转载 作者:可可西里 更新时间:2023-11-01 01:42:05 27 4
gpt4 key购买 nike

我做了一个简单的游戏,你必须躲避障碍物并收集金币。每个硬币会给你 1 分。玩游戏时有一个得分标签。我如何创建一个高分标签,即使他们退出游戏也会记住玩家的高分。我也想知道如何将高分与游戏中心联系起来。

如有任何帮助,我们将不胜感激。

到目前为止,这就是我确定获胜者何时赢得比赛的方式。

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 {
coin.removeFromParent()
playerScore = playerScore + 1
playerScoreUpdate()
}

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

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

}



}

编辑

saveHighScore(100)
var score = 99
if score > highScore() {
saveHighScore(score)
println("New Highscore = " + highScore().description)
} else {
println("HighScore = " + highScore().description ) // "HighScore = 100"
}
score = 127
if score > highScore() {
saveHighScore(score)
println("New Highscore = " + highScore().description) // "New Highscore = 127"
} else {
println("HighScore = " + highScore().description )
}

编辑 2

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

编辑 3

func addHighScoreLabel() {

// Player Score
highScoreLabel.fontName = "DIN Condensed"
highScoreLabel.fontSize = 28
highScoreLabel.fontColor = SKColor.whiteColor()
highScoreLabel.position = CGPoint(x: 500, y: size.height/1.09)
highScoreLabel.text = "HighScore: \(highScore)"
addChild(highScoreLabel)
}

?

最佳答案

var playerScore = 0

func playerScoreUpdate() {
let highScore = NSUserDefaults().integerForKey("highscore")
if playerScore > highScore {
NSUserDefaults().setInteger(playerScore, forKey: "highscore")
}
playerScorelabel.text = "Score: \(playerScore)"
}

playerScore = 200
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") ) // 200

playerScore = 180
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") ) // 200


playerScore = 250
playerScoreUpdate()
println( NSUserDefaults().integerForKey("highscore") ) // 250


highScoreLabel.text = "HighScore: " + NSUserDefaults().integerForKey("highscore").description

关于xcode - 如何使用sprite kit和swift在游戏中设置高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920254/

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