Game Development Reference
In-Depth Information
Switch over to Xcode, and open the GameScene.m file. Add the code in Listing 4-14 at
the bottom of the file, just above the @end line.
Listing 4-14 . This exit collision handler just removes both the player and exit node
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair
*)pair
player:(CCNode *)player
exit:(CCNode *)exit
{
[player removeFromParent];
[exit removeFromParent];
return NO;
}
For now, this method just removes both the player and exit nodes from their parent, effect-
ively deleting them. The return value doesn't play a role here, but it is returning NO to al-
low the two bodies to intersect.
Tip Unlike other physics engines (for example, Box2D), and respectively earli-
er versions of Chipmunk2D (the physics engine used by Cocos2D), it is legal to
remove a colliding node during a collision callback method.
Build and run the project. Move the player toward the exit doughnut. Once they intersect,
they will both disappear and the view will jump to the level's lower-left corner simply be-
cause there's no player to center on anymore.
For now, removing the player is all the exit does. If you don't like it, feel free to comment
out both removeFromParent lines in Listing 4-14 . You'll later come back to this
method to create a popover menu.
Summary
Your game is now set up to move the player through the level using physics forces and
gravity, while impenetrable static physics walls prevent the player from leaving the level
boundaries.
Search WWH ::




Custom Search