Game Development Reference
In-Depth Information
There is one last topic I want to cover before moving on to the coordinate system and an-
chor points: how you search the node tree. Something I have not discussed yet is the
name property of the SKNode . The SKNode.name property is a String property that
can be used to identify a unique sprite or a group of sprites.
An example of this would be to give the unique playerNode a name of Player . As-
suming you were inside the GameScene and you did give the playerNode a name of
Player , you could then search the GameScene for a unique node named Player us-
ing the following line of code:
childNodeWithName("Player")
This method returns an optional SKNode . If more than one child shares the same name,
then the first node in the children array would be returned. If no child with this name was
found, then return would have no value.
Another example might be to name a collection of sprites with the same name. For ex-
ample, you could name a collection of sprites with the same circular texture, such as orb .
You could then search for all sprites with that name and apply the set of actions in one
call. This can be done using SKNode 's enumerateChildNodesWithName() meth-
od.
This method searches for all nodes with the passed-in name and then executes the code in
the block on each node found. Again, assuming you were inside the GameScene and you
have added several sprites all named orb , you could search for all the orb nodes and ex-
ecute a block on each one using the following snippet:
enumerateChildNodesWithName("orb", usingBlock: {
node, stop in
// do something with node or stop
})
Everything looks pretty straightforward here except for the parameters being passed to the
block every time a node with the name orb is found. The first parameter is a reference to
the node that was found—no problem. The second parameter, stop , is a pointer to a
Boolean that can be used to stop the iteration. If you want to stop iterating over each node
found, set the stop parameter's memory property to true like the following example.
stop.memory = true
Search WWH ::




Custom Search