问题描述
我注意到 SKNode 方法 children
和 childNodeWithName:
顾名思义只返回来自查询节点正下方的子节点的结果.即 [root children];
将返回一个包含节点 @[CUBE1, CUBE2, CUBE3]
的 NSArray
.
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]
.
在下图中,我想从 ROOT (SKScene) 级别下降到 SPHERE2,以便我可以快速访问该节点的子级.我希望 [root childNodeWithName:@"SPHERE2"];
会遍历整个层次结构并返回一个指向 SPHERE2 的指针
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
我的问题:是否有什么我遗漏的东西会让我在指定点(即使用节点名称)跳入节点树
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 ...
推荐答案
您可以使用 SKNode 文档,在高级搜索下.
You can use the advanced search syntax described in the SKNode documentation, under Advanced Searches.
这将递归搜索整个节点树以查找名为SPHERE2"的节点,从根节点开始:
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"];
应该注意的是,如果您经常需要这些节点,您应该将它们缓存在 __weak
ivar 或 weak
属性中,因为按名称搜索节点需要一些时间.
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.
这篇关于跳转到节点层次结构中的特定 SKNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!