Search code examples
iosswiftsprite-kitenumerationsknode

Advanced search using enumerateChildNodesWithName method


I am having ten sprites (A0,A2...A9 sprites) and I am enumerating their parent (myNode) using next search string A[0-9] in order to match sprites named A0, A1...A9.

From the docs about using this pattern as search string :

This search string matches any of the current node’s children that are named A0, A1, …, A9.

Here is the code I use:

myNode.enumerateChildNodesWithName("A[0-9]") { sprite, stop in

     //do some stuff here    
}

And this works, like stated in docs. But, let say I have now 20 sprites... When I tried to match sprites named A1, A2, A3...A20, like this:

myNode.enumerateChildNodesWithName("A[0-20]") {...}

it didn't worked... Am I expecting too much of this feature or I am missing something ?

I am able to match desired sprites in a few different ways, for example, by putting them in the same container and enumerating all of the sprites in it, but that is not the point, and I am wondering if there is something that can be done in order to use [lowerBound-upperBound] notation ?


Solution

  • Name your sprites A01-A20 and try A[0-2][1-9]

    Edit: It appears that the search argument is actually a regex. From the declaration of the function:

    name An Xpath style path that can include simple regular expressions for matching node names.

    So ^A[0-9]{1,2}$ will match 'A' followed by one or 2 digits as the complete node name.