2017-08-01 81 views
-1

遵循教程並使以下代碼顯示當前硬幣計數。我如何保存一個新的高分,這樣我可以輸出顯示?保存並輸出高分

我可以計算出/如何輸出/顯示它 - 只需要解決如何首先保存它。

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 
    } 
} 

}

+0

即使在* Related *列中,也有許多相關的[問題和分析器](https://stackoverflow.com/search?q=SpriteKit+save+high+score)。 – vadian

+0

在swift https://github.com/shubh10/JustHUD上試用這個適用於iOS的HUD庫 –

回答

0

你可以使用UserDefaults的幫助下存儲數據量小。

UserDefaults.standard.set(00, forKey: "HighScore")