Game Development Reference
In-Depth Information
ginContact and EndContact methods are defined by Box2D and get called
whenever there's a collision between two bodies.
In the implementation, I merely change the sprite's colors to magenta while the two
bodies are in contact and set it back to white when they're no longer in contact, as
shown in Listing 12-7 .
Listing 12-7. Checking for the Beginning and Ending of Collisions
#import "ContactListener.h"
#import "cocos2d.h"
#import "PhysicsSprite.h"
void ContactListener::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact-> GetFixtureA()-> GetBody();
b2Body* bodyB = contact-> GetFixtureB()-> GetBody();
PhysicsSprite* spriteA = (__bridge PhysicsSprite*)bodyA-> GetUserData();
PhysicsSprite* spriteB = (__bridge PhysicsSprite*)bodyB-> GetUserData();
if (spriteA != NULL && spriteB != NULL)
{
spriteA.color = ccMAGENTA;
spriteB.color = ccMAGENTA;
}
}
void ContactListener::EndContact(b2Contact* contact)
{
b2Body* bodyA = contact-> GetFixtureA()-> GetBody();
b2Body* bodyB = contact-> GetFixtureB()-> GetBody();
PhysicsSprite* spriteA = (__bridge PhysicsSprite*)bodyA-> GetUserData();
PhysicsSprite* spriteB = (__bridge PhysicsSprite*)bodyB-> GetUserData();
if (spriteA ! = NULL && spriteB ! = NULL)
{
spriteA.color = ccWHITE;
spriteB.color = ccWHITE;
 
 
Search Nedrilad ::




Custom Search