Game Development Reference
In-Depth Information
The wildcard parameter refers to any node with a physics body, regardless of its colli-
sion type. Because no other body has a “Collision type” value except the player, the pre-
ceding method will be called whenever the player touches any other body in the level.
You should know that you can't have a callback method with two wildcard parameters.
The second parameter should always be named the same as a Collision type identi-
fier; otherwise, the callback method will never run to begin with. You can't use wild-
card as the second parameter's name, though.
In other words, there is no way to run a callback method every time any two bodies col-
lide. If you've used Cocos2D or physics engines before, you may be used to doing just
that. Then, within the method, decide which types of bodies are colliding and how to pro-
ceed from there. The approach using the Collision type identifier and naming the
callback method parameters allow you to sort out these collision notifications more effi-
ciently and will automatically distribute collision event-handling code to different meth-
ods, based on the bodies' collision types.
The third method parameter in Listing 4-13 could also have been the name of another
body's Collision type , such as exit . For instance, the following method signature
will be used shortly to determine collisions between the player and the exit node:
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair
*)pair
player:(CCNode *)player
exit:(CCNode *)exit;
Tip The method parameter's name before the colon and the local variable's
name after the colon need not be identical like in Listing 4-12 . But it is a best
practice to use the same identifiers in collision callback methods, like in Listing
4-13 , to avoid any confusion.
The collision callback methods always return a BOOL value. If you return YES, you tell
the physics engine that the colliding bodies should collide. If you return NO, the colliding
bodies will pass through each other unaffected. It is, however, more efficient to use the
Categories and Masks to sort out unwanted collisions and thus not even run the collision
callback method. This is preferable in situations where you don't need to process addition-
al collision code for the bodies in question.
Search WWH ::




Custom Search