Game Development Reference
In-Depth Information
Collision Callback Methods
Now it's time to fire up Xcode once more to add code that runs a so-called callback meth-
od whenever specific collision events occur.
Implementing the Collision Delegate Protocol
Before you can receive contact callback messages, there's a little setup necessary in order
to register the GameScene class with the CCPhysicsBody of the level, as referenced by
the _physicsNode ivar.
Open GameScene.h , which you'll find looks pretty basic, like in Listing 4-9 .
Listing 4-9 . The GameScene interface
#import "CCNode.h"
@interface GameScene : CCNode
@end
In order to allow the class to be used as a receiver of physics collision messages, it must
implement the CCPhysicsCollisionDelegate protocol. This is done by simply
appending the protocol in angle brackets at the end of the @interface , as seen in List-
ing 4-10 .
Listing 4-10 . GameScene is now said to adhere to the CCPhysicsCollisionDelegate pro-
tocol
#import "CCNode.h"
@interface GameScene : CCNode <CCPhysicsCollisionDelegate>
@end
In GameScene.m , you can now register self as the collision delegate of the _phys-
icsNode . In the loadLevelNamed: method, add the line highlighted in Listing 4-11 .
Listing 4-11 . Assign the GameScene instance as the _physicsNode's collision delegate
-(void) loadLevelNamed:(NSString*)levelCCB
{
Search WWH ::




Custom Search