Game Development Reference
In-Depth Information
if (recursive)
{
for (CCNode* child in node.children)
{
[self performSelector:selector
onNode:child
withObject:object
recursive:YES];
}
}
}
Tip The method in Listing 6-14 could be added as a category on NSObject in-
stead—in particular, if you find that you have use for it in the future.
The performSelector:onNode:withObject:recursive: method mimics the
behavior of other performSelector methods defined by the NSObject class, except
that it runs the selector only if the given node implements it by testing for re-
spondsToSelector: first.
Note The performSelector:withObject: line may generate a “may
cause a leak” warning. You can safely ignore it as long as the performed select-
or does not return a pointer or id type. Also note that the selector must take a
single parameter—no more and no less. If you need to use other message signa-
tures, you would have to make a variant of the method in Listing 6-14 that takes
no object or two objects, and use either the performSelector or per-
formSelector:withObject:withObject: methods to pass the ob-
jects along.
If the recursive flag is set, the same method will also be run for each of the node's
children and their children. Effectively, every node in the node hierarchy gets a chance to
run the given selector merely by having implemented it.
This is not the most efficient way of passing messages to other nodes, so avoid using this
for frequent events or as a replacement for properly obtaining references of nodes to
which you want to send messages. But it works well as a convenience to distribute rare
messages without knowing which nodes may be interested in receiving that message. The
Search WWH ::




Custom Search