You have to implement a new class for the collision or say contact listener based on some criteria as tag no. i have an example for you.
// ContactListener.h
#import "Box2D.h"
class ContactListener : public b2ContactListener
{
private:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
// ContactListener.mm
#import "ContactListener.h"
#import "cocos2d.h"
#import "BodyNode.h"
#import "GameScene.h"
void ContactListener::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
BodyNode* bNodeA = (BodyNode*)bodyA->GetUserData();
BodyNode* bNodeB = (BodyNode*)bodyB->GetUserData();
if ((bNodeA.tag == GameSceneNodeTagBall && bNodeB.tag == GameSceneNodeTagHole) ||
(bNodeA.tag == GameSceneNodeTagHole && bNodeB.tag == GameSceneNodeTagBall))
{
switch (bNodeA.tag) {
case GameSceneNodeTagBall:
if ([bNodeA isKindOfClass:[Ball class]]) {
Ball* ball = (Ball*)bNodeA;
ball.sprite.visible = NO;
[[GameScene sharedGameScene] gameOver];
}
break;
case GameSceneNodeTagHole:
if ([bNodeB isKindOfClass:[Ball class]]) {
Ball* ball = (Ball*)bNodeB;
ball.sprite.visible = NO;
[[GameScene sharedGameScene] gameOver];
}
break;
default:
break;
}
}
}
}
This is an example of ball and hole collision. You can use this as per your requirement.
Hope this will help you.
Thanks!
to check the collision only for one time :
// ContactListener.mm
#import "ContactListener.h"
#import "cocos2d.h"
#import "BodyNode.h"
#import "GameScene.h"
void ContactListener::BeginContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
BodyNode* bNodeA = (BodyNode*)bodyA->GetUserData();
BodyNode* bNodeB = (BodyNode*)bodyB->GetUserData();
if ((bNodeA.tag == GameSceneNodeTagBall && bNodeB.tag == GameSceneNodeTagHole) ||
(bNodeA.tag == GameSceneNodeTagHole && bNodeB.tag == GameSceneNodeTagBall))
{
switch (bNodeA.tag) {
case GameSceneNodeTagBall:
if ([bNodeA isKindOfClass:[Ball class]] && isBallCollision == NO) {
Ball* ball = (Ball*)bNodeA;
ball.sprite.visible = NO;
isBallCollision = YES;
[[GameScene sharedGameScene] gameOver];
}
break;
case GameSceneNodeTagHole:
if ([bNodeB isKindOfClass:[Ball class]] && isBallCollision == NO) {
Ball* ball = (Ball*)bNodeB;
ball.sprite.visible = NO;
isBallCollision = YES;
[[GameScene sharedGameScene] gameOver];
}
break;
default:
isBallCollision = NO;
break;
}
}
}
}