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

    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?


I found a solution on my own.

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


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

| improve this answer | |

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.