Reputation: 620
In my GameScene.sks
file, I have the player with the GameScene as the parent and a SKLabelNode
as the child of the player. Within the GameScene.sks
file, I have the text as 0 and the following code within GameScene.swift
file
playerHealthLbl = childNode(withName: "playerHealthLbl") as? SKLabelNode
playerHealthLbl?.text = String(200)
The issue here is that whenever I build and run my project, the .SKLableNode.text
is still equal to 0 instead of 200. I made sure that the name in GameScene.sks
file is the same as GameScene.swift
file.
Also, if I switch the SKLabelNode
's parent to the scene itself, then the text changes to 200. Any ideas why? Please advise.
Thanks.
Upvotes: 2
Views: 286
Reputation: 6071
because the label is a child of player not self
childNode(withName: "playerHealthLbl")
is the same as self.childNode(withName: "playerHealthLbl")
and self is the scene (in this instance), and scene doesn't have a child named "playerHealthLbl"
so you want to do player.childNode(withName: "playerHealthLbl")
or childNode(withName: "//playerHealthLbl")
which transverses through all children regardless of layer
Upvotes: 1