Cocos2d Box2d contact listener call different method in collided object

Posted by roma86 on Game Development See other posts from Game Development or by roma86
Published on 2012-07-30T07:34:17Z Indexed on 2012/08/30 3:50 UTC
Read the original article Hit count: 199

I have the building object, wen it hit with other, i want to call some method inside it. Building.mm

-(void)printTest
{
    NSLog(@"printTest method work correctly");
}

in my ContactListener i trying to call it so: ContactListener.mm

#import "ContactListener.h"
#import "Building.h"
void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    Building *buildA = (Building *)bodyA->GetUserData();
    Building *buildB = (Building *)bodyB->GetUserData();
    if (buildB.tag == 1) {
        NSLog(@"tag == 1");
    }
    if (buildA.tag == 2) {
        NSLog(@"tag == 2");
        [buildA printTest];
    }
}

NSLog(@"tag == 2"); work correctly, but [buildA printTest]; get error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite printTest]: unrecognized selector sent to instance 0x18595f0'

Obviously I'm referring to the wrong object. How i can get different method and property in contacted objects from contactlistener class? Thanks.

© Game Development or respective owner

Related posts about collision-detection

Related posts about cocos2d-iphone