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???

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Rony
  • 1,229
  • 4
  • 22
  • 42
  • 1
    Please `NSLog(@"%@", _label);` after `shakeCounter++;` and show the output. – kennytm Aug 04 '10 at 08:49
  • 1
    This question is related to [Unable to call an Objective C method from a C function](http://stackoverflow.com/questions/304885/unable-to-call-an-objective-c-method-from-a-c-function). – apaderno Aug 04 '10 at 08:50
  • yes....i saw this and i got null.... – Rony Aug 04 '10 at 08:51

5 Answers5

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.

hardcore
  • 21
  • 2
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...........

Rony
  • 1,229
  • 4
  • 22
  • 42
  • Actually, no you haven't solved the problem. The fact that it appears to work just shows you are doing something fundamentally wrong. – JeremyP Aug 04 '10 at 09:58
  • I'm receiving runtime memory leaked warnings with ARC enabled using above code. http://stackoverflow.com/questions/11840943/runtime-memory-leaked-warnings-when-executing-objective-c-code-within-c-code-wit – docchang Aug 07 '12 at 07:22
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];
dreamlax
  • 93,976
  • 29
  • 161
  • 209
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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
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

Tim
  • 5,024
  • 2
  • 30
  • 58