保存并输出高分

时间:2017-08-01 09:51:27

标签: ios swift swift3

遵循教程并使用以下代码显示当前的硬币计数。如何保存新的高分,以便我可以输出显示?

我可以弄清楚如何/在哪里输出/显示它 - 只需要先弄清楚如何保存它。

import SpriteKit

class HUD: SKNode {
var textureAtlas = SKTextureAtlas(named:"HUD")
var coinAtlas = SKTextureAtlas(named: "Environment")
// An array to keep track of the hearts:
var heartNodes:[SKSpriteNode] = []
// An SKLableNode to print the coin score:
let coinCountText = SKLabelNode(text: "000000")
let restartButton = SKSpriteNode()
let menuButton = SKSpriteNode()
func createHudNodes(screenSize:CGSize) {
    let cameraOrigin = CGPoint(x: screenSize.width / 2,
                               y: screenSize.height / 2)
    // Create the coin counter:
    // First, create and position a bronze coin icon:
    let coinIcon = SKSpriteNode(texture: coinAtlas.textureNamed("coin-bronze"))
    // Size and position the coin icon:
    let coinPosition = CGPoint(x: -cameraOrigin.x + 23, y: cameraOrigin.y - 23)
    coinIcon.size = CGSize(width: 26, height: 26)
    coinIcon.position = coinPosition
    // Configure the coin text label:
    coinCountText.fontName = "AvenirNext-HeavyItalic"
    let coinTextPosition = CGPoint(x: -cameraOrigin.x + 41, y: coinPosition.y)
    coinCountText.position = coinTextPosition
    // These two properties allow you to align the
    // text relative to the SKLabelNode's position:
    coinCountText.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
    coinCountText.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center
    // Add the text label and coin icon to the HUD:
    self.addChild(coinCountText)
    self.addChild(coinIcon)
}

func setCoinCountDisplay(newCoinCount:Int) {
    // We can use the NSNumberFormatter class to pad
    // leading 0's onto the coin count:
    let formatter = NumberFormatter()
    let number = NSNumber(value: newCoinCount)
    formatter.minimumIntegerDigits = 6
    if let coinStr = formatter.string(from: number) {
        // Update the label node with the new count:
        coinCountText.text = coinStr
    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用UserDefaults的帮助来存储少量数据。

UserDefaults.standard.set(00, forKey: "HighScore") 
相关问题