Navigate to a specific SKNode in a node hierarchy

I noticed that the SKNode children and childNodeWithName: methods, since the name implies only the results of returning from the children directly under the requested node. that is, [root children]; returns an NSArray containing the nodes @[CUBE1, CUBE2, CUBE3] .

In the diagram below, I want to switch from the ROOT (SKScene) level to the SPHERE2 level so that I can quickly access these child nodes. I was hoping that [root childNodeWithName:@"SPHERE2"]; will go through the entire hierarchy and return a pointer to SPHERE2

enter image description here

MY QUESTION: There is something that I missed that will allow me to go to the node tree at the specified point (i.e. using the name node)

I could use properties to store pointers to important positions in the tree, and then use them to access and process any child nodes, so this is an option ...

+6
source share
1 answer

You can use the advanced search syntax described in the SKNode Documentation in the Advanced Search Queries section.

This will search recursively for the entire node tree for the node named "SPHERE2", starting with the root of the node:

 SKNode* sphere2 = [root childNodeWithName:@"//SPHERE2"]; 

If you know the path to a specific node, you can also use this quite easily:

 SKNode* triangle3 = [root childNodeWithName:@"/CUBE3/SPHERE2/TRIANGLE3"]; 

It should be noted that you often need these nodes, you must cache them in the __weak ivar or weak property, because it takes a little time to find nodes by name.

+12
source

Source: https://habr.com/ru/post/957129/


All Articles