Game Development Reference
In-Depth Information
Caution NSPointerArray is available only on devices running iOS 6.0 or
newer.
With the triggers array set up, all that's left to do is pass in self in the call to ad-
dPointer: , but not without casting it to (void*) because NSPointerArray stores
generic void* pointer values. The (void*) cast doesn't alter the object reference in
any way; it's merely a way of stating to the compiler that the self object can safely be
represented as a void* pointer.
Finding and Storing Targets
The remaining lines of Listing 5-8 that we haven't discussed so far are reprinted in Listing
5-10 for the sake of clarity, focus, and zen.
Listing 5-10 . Searching for targets
if ([targetArrays objectForKey:_name] == nil)
{
NSPointerArray* targets = [NSPointerArray
weakObjectsPointerArray];
[targetArrays setObject:targets forKey:_name];
[self addTargetsTo:targets searchInNode:self.scene];
}
Notice that targets are searched only when there's no corresponding entry of the given
_name in the targetArrays dictionary. This is to prevent searching the same targets
multiple times in cases where you have multiple triggers of the same name. Only the first
trigger needs to search for targets because the other triggers will refer to the same targets.
If there's no array with the given _name in targetArrays, the code in Listing 5-10
will create an NSPointerArray storing weak pointer references and assign it to tar-
gets . The same array is also assigned to targetArrays using the trigger _name as
its key. Next a method is called that performs the actual search recursively, starting with
the self.scene node. It adds all the target nodes of the same name to the targets
array.
The addTargetsTo:searchInNode: method is shown in Listing 5-11 —you should
add this method to your Trigger.m file just below the onEnter method.
Search WWH ::




Custom Search