0
\$\begingroup\$

I have the building object, when it collides with another object I want to call some method inside it. Building.mm

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

in my ContactListener I'm 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?

\$\endgroup\$
1
\$\begingroup\$

I found a solution on my own.

When I create the body, I forgot to set the UserData correctly.

body->SetUserData(self);

Adding the line above (in Building.mm) solved the problem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.