Game Development Reference
In-Depth Information
Listing 5-11 . Recursively searching for and gathering target nodes
-(void) addTargetsTo:(NSPointerArray*)targets
searchInNode:(CCNode*)node
{
for (CCNode* child in node.children)
{
if ([child.name isEqualToString:_name])
{
if ([child
conformsToProtocol:@protocol(TriggerDelegate)])
{
[targets addPointer:(void*)child];
}
}
[self addTargetsTo:targets searchInNode:child];
}
}
The addTargetsTo:searchInNode: method runs recursively and tries to collect all
instances of nodes given a specific name and whose class conforms the Trigger-
Delegate protocol. You can't use getChildByName: here because that will return
only the first child node with the given name, but you need a list of all nodes of the same
name.
If a target node has the correct name and conforms to the TriggerDelegate protocol,
a reference to the target node is added to the targets array.
The last line continues the recursive search by calling the addTarget-
sTo:searchInNode: method again, but with child as the parameter. This ensures
that every node in the scene is processed once and not a single one will be missed.
It also means the trigger system in its current version won't recognize nodes added after
loading the GameScene.ccb file. If you need this, I'll leave it as an exercise for you to
do.
You could add a +(void) addTarget:(CCNode*)target method to the Trig-
ger class that assigns the target node, based on its name, to the correct targets ar-
ray stored in targetArrays . You should verify that the target node conforms to the
TriggerDelegate protocol before adding it. To add a target node, you could then call
Search WWH ::




Custom Search