Game Development Reference
In-Depth Information
[Trigger addTarget:aTargetNode] from any other class, provided that you did
add #import "Trigger.h" to that class' implementation file.
Forwarding Trigger Events to Targets
What's left is to inform target nodes when the trigger is activated. This is where the
triggerActivatedBy: method in Listing 5-12 comes into play.
Add the code in Listing 5-12 to Trigger.m below the addTarget-
sTo:searchInNode: method.
Listing 5-12 . Sending trigger messages to targets
-(void) triggerActivatedBy:(CCNode*)activator
{
NSPointerArray* targets = [targetArrays
objectForKey:_name];
for (id target in targets)
{
[target didTriggerWithNode:activator];
}
if (_repeatsForever == NO)
{
NSPointerArray* triggers = [triggerArrays
objectForKey:_name];
[self updateTriggers:triggers];
[self cleanupTriggers:triggers];
}
}
Based on the trigger's _name , the list of targets is obtained from targetArrays .
All targets are enumerated to send the didTriggerWithNode: message to each tar-
get, which allows each target to run custom code when triggered.
Declaring the target variable with the id keyword in the for loop allows you to send
the didTriggerWithNode: message without the compiler complaining that the se-
lector is not declared. If for some reason you need to declare target as CCNode* , you
will have to include the TriggerDelegate protocol to tell the compiler that you are
Search WWH ::




Custom Search