Box2D联系听众?

时间:2011-10-23 02:51:30

标签: ios collision-detection box2d

我正在尝试在游戏中使用Box2D联系人监听器。我知道Ray Wenderlich有一个如何做到这一点的教程,但我发现它令人困惑,我想看看是否有人可以帮我解释如何做到这一点。

我是否需要为此联系人监听器设置一个特定的类,或者它只是我的CCScene类中的几个基本代码行?

有人可以对此提供一些见解吗?

感谢!!!

1 个答案:

答案 0 :(得分:3)

您必须为碰撞实现一个新类,或者根据某些标准将联系人监听器称为标记号。我有一个例子给你。

// 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;
            }
        }
    }
}

这是球洞碰撞的一个例子。您可以根据自己的要求使用它。

希望这会对你有所帮助。

谢谢!

仅检查碰撞一次:

// 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;
                }
            }
        }
    }