這是如何設置SKLabelNode無論是作爲一個屬性的示例和本地和acce SS這些變量後面的代碼:
import SpriteKit
class GameScene: SKScene {
let label = SKLabelNode(fontNamed: "ArialMT")
override func didMoveToView(view: SKView) {
label.text = "This is a label defined as a property of scene";
label.fontSize = 35;
label.fontColor = .blackColor()
label.position = CGPoint(x:frame.midX, y:frame.midY)
addChild(label)
let anotherLabel = SKLabelNode(fontNamed: "ArialMT")
anotherLabel.text = "This is a label defined localy and added to a node tree"
anotherLabel.name = "anotherLabel"
anotherLabel.fontSize = 35;
anotherLabel.fontColor = .whiteColor()
anotherLabel.position = CGPoint(x:frame.midX, y:frame.midY - 100.0)
addChild(anotherLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.label.text = "Your text"
if let anotherLabel = childNodeWithName("anotherLabel") as? SKLabelNode {
anotherLabel.text = "Your text"
}
}
}
在財產的情況下,訪問也很簡單self.label.text = "..."
。對於添加到節點樹的局部變量,您可以使用其名稱屬性稍後使用childNodeWithName
方法查找它。
如果您想了解更多有關該錯誤的信息,請提供更多有關此代碼放置位置的上下文(例如,它是場景/某個其他類的屬性,這可能是錯誤的,或者它是通過某種方法在本地定義的等等)。 – Whirlwind
@Whirlwind基本上,我只是將該行放入GameScene類中,在這裏我還放置了其他變量和定義。但我正在尋找一個機會來做這樣的事情:scoreLabel.text =「3」 – boehmatron
我把計時器的設置放在一個單獨的函數中,並在「didMove」上調用它,解決了問題:)謝謝! – boehmatron