这是如何设置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