是否可以更新 SKLabelNode?
Is it possible to update an SKLabelNode?
我正在尝试更新 coinLabel,以便在点击 "life" 节点时,它将更新 SKLabelNode 并在从总数中减去它们后显示正确的硬币。我是编码新手,所以如果还有其他问题,请回复!谢谢!
import Foundation
import SpriteKit
class ShopScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
mainMenu.text = "Main Menu"
mainMenu.fontSize = 100
mainMenu.fontColor = SKColor.darkGray
mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
mainMenu.zPosition = 1
mainMenu.name = "Main Menu"
self.addChild(mainMenu)
let Life = SKLabelNode(fontNamed: "The Bold Font")
Life.text = "Click here to buy 1 life!"
Life.fontSize = 130
Life.fontColor = SKColor.darkGray
Life.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.6)
Life.zPosition = 1
Life.name = "Life"
self.addChild(Life)
let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Main Menu" {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
}
if nodeITapped.name == "Life" {
if coinNumber > 10 {
lifeNumber += 1
coinNumber -= 10
defaults.set(coinNumber, forKey: "coinNumberSaved")
defaults.set(lifeNumber, forKey: "lifeNumberSaved")
return
}
}
}
}
}
只需再次更新点击识别器中的文本即可进行更改
coinLabel.text = "Coins: \(coinNumber)"
您将需要确保在 init 之外有对 coinLabel 的引用,以便您可以在它之外对其进行操作。
在您的 ShopScene 顶部,您可以执行...
class ShopScene: SKScene {
var coinLabel = SKLabelNode()
//your code
}
然后当您在 didMove(to view:) 函数中转到 init 时,改为执行...
coinLabel = SKLabelNode(fontNamed: "The Bold Font")
这样您就可以在整个代码中引用它。尽管将来您会考虑创建 SKNode 的子类来处理所有 UI 元素,这样您就不必在 GameScene 中初始化所有内容。
我正在尝试更新 coinLabel,以便在点击 "life" 节点时,它将更新 SKLabelNode 并在从总数中减去它们后显示正确的硬币。我是编码新手,所以如果还有其他问题,请回复!谢谢!
import Foundation
import SpriteKit
class ShopScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
mainMenu.text = "Main Menu"
mainMenu.fontSize = 100
mainMenu.fontColor = SKColor.darkGray
mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
mainMenu.zPosition = 1
mainMenu.name = "Main Menu"
self.addChild(mainMenu)
let Life = SKLabelNode(fontNamed: "The Bold Font")
Life.text = "Click here to buy 1 life!"
Life.fontSize = 130
Life.fontColor = SKColor.darkGray
Life.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.6)
Life.zPosition = 1
Life.name = "Life"
self.addChild(Life)
let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
coinLabel.text = "Coins: \(coinNumber)"
coinLabel.fontSize = 100
coinLabel.fontColor = SKColor.black
coinLabel.zPosition = 1
coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
self.addChild(coinLabel)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeITapped = atPoint(pointOfTouch)
if nodeITapped.name == "Main Menu" {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTrasition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
}
if nodeITapped.name == "Life" {
if coinNumber > 10 {
lifeNumber += 1
coinNumber -= 10
defaults.set(coinNumber, forKey: "coinNumberSaved")
defaults.set(lifeNumber, forKey: "lifeNumberSaved")
return
}
}
}
}
}
只需再次更新点击识别器中的文本即可进行更改
coinLabel.text = "Coins: \(coinNumber)"
您将需要确保在 init 之外有对 coinLabel 的引用,以便您可以在它之外对其进行操作。 在您的 ShopScene 顶部,您可以执行...
class ShopScene: SKScene {
var coinLabel = SKLabelNode()
//your code
}
然后当您在 didMove(to view:) 函数中转到 init 时,改为执行...
coinLabel = SKLabelNode(fontNamed: "The Bold Font")
这样您就可以在整个代码中引用它。尽管将来您会考虑创建 SKNode 的子类来处理所有 UI 元素,这样您就不必在 GameScene 中初始化所有内容。