0
votes

im new to programming, i have tried the ray wenderlich tutorial but im still confused.

i have a sprite called rock, and a sprite called player, im trying to detect collision between them. but im extremely confused. in need of some help.

-(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];

they are the two sprites, they spawn and position correctly, i only need to detect collision

1
Please correct your code. It's cluttered and unreadable.Mariusz Jamro
ok, just uncluttered it... sorrysahil

1 Answers

1
votes

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!