Game Development Reference
In-Depth Information
Listing 5-14
.
Responding to collisions between player and trigger nodes
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair
*)pair
player:(CCNode *)player
trigger:(Trigger *)trigger
{
[trigger triggerActivatedBy:player];
// return NO to allow the bodies to pass through each
other
return NO;
}
Notice that the third parameter is a pointer to a
Trigger
instance rather than a
CCNode
instance. You can declare the parameter to be a
Trigger*
instead of
CCNode*
under
the assumption that the trigger parameter will always be a
Trigger
class instance. If it is
not, it will result in a “
unrecognized selector sent to instance
” error.
You will also have to import the
Trigger.h
in the
GameScene.m
file. Add the fol-
lowing line at the top of the
GameScene.m
file, just below the other
#import
lines:
#import "Trigger.h"
Importing the
Trigger.h
file will make the
Trigger
class known to the
GameScene
class. This will get rid of the “
Use of undeclared identifier 'Trigger'
”
error.
Triggering Target Nodes
Every target node that's supposed to be activated by a trigger needs to have the following:
A custom class assigned to the node in SpriteBuilder.
The custom class' header needs to
#import "Trigger.h"
.
The custom class'
@interface
needs to adopt the
TriggerDelegate
protocol.
The custom class'
@implementation
needs to have a
-(void)
didTriggerWithNode:(CCNode*)activator
method with the de-
sired functionality.
Search WWH ::
Custom Search