views:

68

answers:

4

Hi,

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

A: 

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
Actually, no you haven't solved the problem. The fact that it appears to work just shows you are doing something fundamentally wrong.
JeremyP
A: 

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
A: 

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
A: 

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 Kemp