Andy Barnard
Andy Barnard

Reputation: 706

SKNode failing to find childNodeWithName:

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.

Upvotes: 1

Views: 1500

Answers (2)

Raymond
Raymond

Reputation: 21

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.

Upvotes: 2

Andy Barnard
Andy Barnard

Reputation: 706

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.

Upvotes: 3

Related Questions