I am making a game with SpriteKit.
I am trying to add a SKLabelNode (pointsLabel), and I have got it to work if I don't set label.isHidden = true. However, I want the Label to be hidden until touchesBegan, but it seems like I can't recall the LabelNode in this function, so the label remains hidden.
How do I get it to work?
This is the code:
class GameScene: SKScene {
private var pointsLabel: SKLabelNode?
override func didMove(to view: SKView) {
pointsLabelShow()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Get label node from scene and store it for use later
self.label = self.childNode(withName: "//pointsLabel") as? SKLabelNode
if let label = self.label {
label.isHidden = false
let fade = SKAction.fadeIn(withDuration: 0.2)
let sequence = SKAction.sequence([fade])
label.run(sequence)
}
}
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.label = self.childNode(withName: "//pointsLabel") as? SKLabelNode
if let label = self.label {
label.position.y = ballHeight + 840
}
func pointsLabelShow() {
let pointsLabel = SKLabelNode(fontNamed: "orkneymedium")
pointsLabel.isHidden = true
pointsLabel.text = "000"
pointsLabel.fontSize = 50
pointsLabel.fontColor = SKColor.white
self.addChild(pointsLabel)
}
}
}
Try to put a name to the SKLabelNode, suppose “pointsLabel"
If you want the node only when touched, in your TouchsBegan method, do this:
if you want the node to come in a touch anywhere, try to put the node in a local variable
Hope this helps