SKNode failing to find childNodeWithName:

旧城冷巷雨未停 提交于 2019-12-11 10:29:29

问题


I am searching for an SKNode in an SKScene by using the childNodeWithName: method. To set the name I am using an NSManagedObjectID's URI string representation, obtained like so:

[[managedObjectID URIRepresentation] absoluteString];

This gives a node name of:

x-coredata://0856426F-EA66-4D28-A88A-FF49225F69B6/Object/p4

However, the childNodeWithName: method returns nil. The node is clearly present when inspecting in the debugger, along with two other nodes:

<__NSArrayI 0x9933eb0>(

<SKSpriteNode> name:'(null)' texture:['nil'] position:{160, 284} size:{320, 568} rotation:0.00,

<SKSpriteNode> name:'x-coredata://0856426F-EA66-4D28-A88A-FF49225F69B6/Object/p4' texture:['nil'] position:{31, 537} size:{50, 50} rotation:0.00,

<SKSpriteNode> name:'x-coredata://0856426F-EA66-4D28-A88A-FF49225F69B6/Object/p1' texture:['nil'] position:{217, 416} size:{50, 50} rotation:0.00

)

And to reinforce this, if I set the node name to something like "testingName", then the node is returned as expected.

This leads me to believe that Sprite Kit doesn't like something about the name I have used. Is anyone aware of naming restrictions, or perhaps has an idea as to why a URL format would give a problem here?

Thanks.


回答1:


Answer here by @kylefuller.

From Apple:

The search uses common regular expression semantics.

"//" - When placed at the start of the search string, this specifies that the search should begin at the root node and be performed recursively across the entire node tree. It is not legal anywhere else in the search string.




回答2:


I thought I would add something regarding this issue as I wasted a few hours on this.

My project has a Sprite kit scene graph as follows:

SKScene-> SKNode(myWorld)->SKNode(camera) as per Apple's example. However when using

var cameraNode = self.childNodeWithName("camera")

the node being returned was always nil. Apple documentation didn't mention the '//' stated by Andy's answer. It seems that the function childNodeWithName will only search the children of the node specified (in this case the scene). As soon as I found this post I changed the line to

var cameraNode = self.childNodeWithName("//camera")

it can now find the camera node regardless of it's level in the scenegraph.



来源:https://stackoverflow.com/questions/22874625/sknode-failing-to-find-childnodewithname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!