7

I have noticed that the SKNode methods children and childNodeWithName: as the name implies only return results from the children directly below the queried node. i.e. [root children]; will return an NSArray containing the nodes @[CUBE1, CUBE2, CUBE3].

In the diagram below I want to get from the ROOT (SKScene) level down to SPHERE2 so that I can quickly access that nodes children. I was hoping that [root childNodeWithName:@"SPHERE2"]; would traverse the whole hierarchy and return a pointer to SPHERE2

enter image description here

MY QUESTION: Is there something that I have missed that will let me jump into a node tree at a specified point (i.e. using the node name)

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

1 Answers1

12

You can use the advanced search syntax described in the SKNode documentation, under Advanced Searches.

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

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

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

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

It should be noted if you need those nodes often, you should cache them in a __weak ivar or weak property because searching nodes by name takes a little while.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Perfect, I don't know how I missed that I have supposedly read all that. Much appreciated Steffen, thanks for the heads up. – fuzzygoat Oct 31 '13 at 16:18
  • OK, but how to find all children named "MyChildren" of the node "Me" while we don't know where "Me" is placed in the tree? – Krzysztof Przygoda Mar 05 '15 at 19:37
  • You would use "//Me/MyChildren" to search the entire tree for nodes named "MyChildren" that have parent named "Me". – Graham Perks Nov 02 '15 at 17:00