0
到目前爲止,這是我所做的,但我的高分並沒有改變。如何使用UserDefaults快速保存高分到遊戲?
林離開了所有無關的變量等
如果有人能幫我,將是非常美妙。我一直在努力做到這一點像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"))"
}
UserDefaults.standard.synchronize()是舊的;不再需要將對象保存到UserDefaults。否則就是好回答! – penatheboss
感謝您的快速回復!不幸的是它沒有工作:(@penatheboss你會知道如何在新的Swift 3中做到這一點?任何幫助表示讚賞。 – niX
@niX這不是我的答案,但它仍然可以工作。 ? – penatheboss