如何在游戏中使用 sprite kit 和 swift 设置高分 [英] How to set a high score in game with sprite kit and swift

查看:25
本文介绍了如何在游戏中使用 sprite kit 和 swift 设置高分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a simple game where you have to dodge obstacles and collect coins. Each coin will give you 1 point. While playing the game there is a score label. How can I create a high score label that will remember the players high score even when they exit the game. I am also wondering how I can connect the high score with the game center.

任何帮助将不胜感激.

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

So far this is how I determine when the winner has won a game.

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

这篇关于如何在游戏中使用 sprite kit 和 swift 设置高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆