gpt4 book ai didi

ios - 如何使用 swift 将高分保存在 sprite kit 中?

转载 作者:行者123 更新时间:2023-11-28 07:05:16 25 4
gpt4 key购买 nike

<分区>

我是编程的初学者。在下面的代码中,我想做的是保存高分,但我做不到。正如您将使用的那样,我正在使用 NSUserDefaults,但一旦游戏结束,它就不会显示在屏幕上。任何帮助将不胜感激。我希望最高分显示在屏幕上,但它没有。

WT 类:SKScene {

let showMessage = SKLabelNode()
let tryAgain = SKLabelNode()
var highScore = SKLabelNode()



override func didMoveToView(view: SKView) {

backgroundColor = SKColor.blackColor()

showMessage.fontName = "Noteworthy-Light"
showMessage.fontSize = 55
showMessage.fontColor = SKColor.redColor()
showMessage.text = "Time's Up!"
showMessage.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
showMessage.name = "show Message"
showMessage.hidden = false
showMessage.zPosition = 100

self.addChild(showMessage)

tryAgain.fontName = "Noteworthy-Light"
tryAgain.fontSize = 44
tryAgain.fontColor = SKColor.greenColor()
tryAgain.text = "Try Again!"
tryAgain.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2 - 200)
tryAgain.name = "T.A"

self.addChild(tryAgain)

highScore = SKLabelNode(fontNamed: "Noteworthy-Light")
highScore.text = "0"
highScore.fontColor = SKColor.whiteColor()
highScore.fontSize = 35
highScore.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
highScore.name = "hs"


addChild(highScore)

var ActionOne = SKAction.scaleBy(0.9, duration: 0.3)

var ActionSequence = SKAction.sequence([ActionOne, ActionOne.reversedAction(), ActionOne, ActionOne.reversedAction()])

tryAgain.runAction(SKAction.repeatActionForever(ActionSequence))


}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

var touch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)

if (node.name == "T.A"){

let myScene = Game(size: self.size)
myScene.scaleMode = scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)

}

}

func saveHighscore(highScore:Int){


if let currentHighscore:Int = NSUserDefaults.standardUserDefaults().valueForKey("highScore") as? Int{

if(highScore > currentHighscore){

NSUserDefaults.setValue(highScore, forKey: "highScore")
}
}else{

NSUserDefaults.setValue(highScore, forKey: "highScore")
}
}

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