Box2D 联系监听器?

发布于 2024-12-11 07:27:20 字数 199 浏览 0 评论 0原文

我正在尝试在我的游戏中使用 Box2D 接触侦听器。我知道 Ray Wenderlich 有一个关于如何执行此操作的教程,但我发现它令人困惑,我想看看是否有人可以帮助向我解释如何执行此操作。

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

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

谢谢!!!

I am trying to use the Box2D contact listener in my game. I know Ray Wenderlich has a tutorial on how to do this but I find it confusing and I wanted to see if someone could help explain to me how to do this.

Do I need to have a specific class just for this contact listener or can it just be a few basic lines of code in my CCScene class?

Can someone offer some insight on this?

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-12-18 07:27:20

您必须为碰撞实现一个新类,或者根据某些标准(如标签号)说接触侦听器。我有一个例子给你。

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

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;
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文