box2d碰撞检测,代码

时间:2012-02-13 15:09:26

标签: cocos2d-iphone box2d-iphone

我是编程的新手,我已经尝试过ray wenderlich教程,但我仍然感到困惑。

我有一个名为rock的精灵,还有一个名为player的精灵,我试图检测它们之间的碰撞。但我非常困惑。需要一些帮助。

-(void)addRock {

CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
[self addChild:rock];

// Determine speed of the sprite
int actualDuration = 5;//speed of sprite

}

- (id)init {

if ((self=[super init])) {

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        CCSprite *player = [CCSprite spriteWithFile:@"Player.png" 
                                     rect:CGRectMake(0, 0, 27, 40)];

        player.position = ccp(winSize.width/2, winSize.height/4+15);

        [self addChild:player];

        [self schedule:@selector(gameLogicRock:) interval:0.2];

它们是两个精灵,它们正确地产生和定位,我只需要检测碰撞

1 个答案:

答案 0 :(得分:1)

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

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

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

希望这会对你有所帮助。

谢谢!