Modifying a child node thru parent node programmatically

Hi,

How do i modify "label" node from parent "block" node thru an instance of "block" in the code?




this in my code,


//this is my instance

var myBlock = self .childNode(withName: "//block") as ? SKSpriteNode


i want to change the value of "label" node thru "myBlock"..?



PS:

For some reason, the attached image is not showing up when I clicked post.

just in case it's not showing up.. my gamescene objects is arranged like this:


➕ Scene

➕ block

➕ label


the label node is a child node of block node.




thanks!
xenogaia2003

Accepted Reply

OK, so label is not the name.

Can you show code where you create this label subnode ?


Typically, you should get the node children

myNode!.children

which is an array of nodes and search for label here (if there is only one subnode, it is [0]


In that case, setting

myBlock!.children[0]

to new value should make it.


For instance to change its name:

myBlock!.children[0].name = "a new label"

Replies

What is label ? Do you mean the name ?


If so, just call

myBlock?.name

or

if myBlock != nil {
     myBlock!.name
}

OK, so label is not the name.

Can you show code where you create this label subnode ?


Typically, you should get the node children

myNode!.children

which is an array of nodes and search for label here (if there is only one subnode, it is [0]


In that case, setting

myBlock!.children[0]

to new value should make it.


For instance to change its name:

myBlock!.children[0].name = "a new label"

label is the name of a "label node" under parent "block" which is a sprite node.


in my code,


var myBlock = self.childNode(withName: "//block") as ? SKSpriteNode


myBlock points to the parent "block" (skspriteNode).


i want to access "label" (label node) to modify the value, say i want to put a new word, for example.



thanks!

Carl

So, did you try what I proposed ?

I would need to see the code where label is created.

Thanks, Master Claude31!