Game Development Reference
In-Depth Information
}
}
b2Contact contains all the contact information, including two sets of everything suf-
fixed with A and B . These are the two contacting bodies; no differentiation is made as
to which is colliding with the other—they're both simply colliding with each other. If,
for example, you have an enemy colliding with a player's bullet, you'd want to damage
the enemy, not the bullet. It's up to you to determine which is which. Also keep in
mind that the contact methods may be called multiple times per frame, once for each
contact pair.
It's a bit convoluted to get to the sprite from the contact through the fixture to the body
and then get the user data from that. The Box2D API reference certainly helps you find
your way through the hierarchy, and with a little experience it'll become second nature.
To actually get the ContactListener connected with Box2D, you have to add it to
the world. In HelloWorldLayer , import the ContactListener.h header file
and add a ContactListener* contactListener to the class as a member
variable:
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import "ContactListener.h"
...
@interface HelloWorldLayer : CCLayer
{
CCTexture2D* spriteTexture;
b2World* world;
ContactListener* contactListener;
GLESDebugDraw* debugDraw;
}
...
@end
In the initPhysics method of HelloWorldLayer , you can then create a new
ContactListener instance and set it as the contact listener for the world:
Search Nedrilad ::




Custom Search