1

I have a cpp class like that..

class MyContactListener : public b2ContactListener
{
    int countContact;
    ///this is an objective c class...
    HelloWorld *hel;


        public:
    void EndContact(b2Contact* contact)
    {
            ///initialize objective c object
        hel=[[HelloWorld alloc] autorelease];
            ///call objective c method.........
        [hel beginContact];

    }

 };

inside cpp class i call a objective c method.the objective c method looks like..

-(void )beginContact
{ 
    shakeCounter++;
    [_label setString:[NSString stringWithFormat:@"%d",shakeCounter]];


}

The objective c method get called....and also the variable shakeCounter increased.....but _label string is not updated...._label is initialized properly and work properly if i called the objective c method from objective c class using self....

Can anyone help???

3
  • 1
    Please NSLog(@"%@", _label); after shakeCounter++; and show the output. Commented Aug 4, 2010 at 8:49
  • 1
    This question is related to Unable to call an Objective C method from a C function. Commented Aug 4, 2010 at 8:50
  • yes....i saw this and i got null.... Commented Aug 4, 2010 at 8:51

5 Answers 5

2

Use a Delegate in the Contact listener to call the methods on the objective-c class:

class MyContactListener : public b2ContactListener
{
    public:
    MyDelegateClass *delegate;

    void BeginContact(b2Contact* contact) { 
        [delegate beginContact:contact];
    }

    void EndContact(b2Contact* contact) { 
        [delegate endContact:contact];
    }

    void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) { 
        [delegate preSolve:contact manifold:oldManifold];
    }

    void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)  {  
        [delegate postSolve:contact impulse:impulse];
    }
};

Declare a Protocol for the Delegate:

#import "Box2D.h"

@protocol B2ContactListener <NSObject>

-(void) beginContact:(b2Contact*) contact; 
-(void) endContact:(b2Contact*) contact;
-(void) preSolve:(b2Contact*) contact manifold:(const b2Manifold*) oldManifold; 
-(void) postSolve:(b2Contact*) contact impulse:(const b2ContactImpulse*) impulse; 

@end

Declare the interface that implements the protocol:

@interface MyDelegateClass: CCLayer <B2ContactListener> {
  //interface code here. 
}
@end

@implementation MyDelegateClass 

-(void) beginContact:(b2Contact*) contact {
    //implement your code here
} 
-(void) endContact:(b2Contact*) contact {
    //implement your code here
} 
-(void) preSolve:(b2Contact*) contact manifold:(const b2Manifold*) oldManifold{
    //implement your code here
}  
-(void) postSolve:(b2Contact*) contact impulse:(const b2ContactImpulse*) impulse{
    //implement your code here
} 
@end

Construct the delegate and the assign it in your code. Set it in your world object:

MyContactListener *listener = new MyContactListener();
listener->delegate = self; 
world_->SetContactListener(listener); 

Now your objective-c class will receive the events.

Sign up to request clarification or add additional context in comments.

Comments

0

oww....i solved it....

just put this in top of project

#define PTM_RATIO 32
#import "HelloWorldScene.h"

id refToSelf;

and initialize this in onload or something like that...

self = [super init];
refToSelf = self;

Now call the objective c method using this....

    [refToSelf beginContact];

it will work...........

2 Comments

Actually, no you haven't solved the problem. The fact that it appears to work just shows you are doing something fundamentally wrong.
I'm receiving runtime memory leaked warnings with ARC enabled using above code. stackoverflow.com/questions/11840943/…
0

I'm not sure if it is the source of your problem, but this line:

hel=[[HelloWorld alloc] autorelease];

Should be:

hel=[[[HelloWorld alloc] init] autorelease];

Comments

0

Your code is totally confused. I assume that you want to create the Objective-C object inside endContact and persist it until some later point. At the moment you are creating a new object each time but you are not initialising it at all. Your code is never going to work.

The C++ method should probably look something like:

void EndContact(b2Contact* contact)
{
    // release old object and initialize a new one
    [hel release];
    hel = [[HelloWorld alloc] init];
        ///call objective c method.........
    [hel beginContact];

}

or

void EndContact(b2Contact* contact)
{
    HelloWorld* hel  = [[HelloWorld alloc] init];
    [hel beginContact];
    [hel release];
}

Depending on how long you want your hel object to last for.

I don't know why _label is not being updated. You'll need to show us your code for initialising it to answer that.

Comments

0

Few things:

Is _label an NSString, or an NSMutableString? An NSString won't respond to setString, because it is fixed at creation.

This:

self = [super init];
refToSelf = self;

Is odd, to say the least. You're basically saying self = self here. You never need to call init on your self.

I'd strongly recommend having a look at Apple's excellent intro here: The Objective C Language

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.