开发者

call objective c method from cpp method

开发者 https://www.devze.com 2023-01-10 08:11 出处:网络
I have a cpp class like that.. class MyContactListener : public b2ContactListener { int countContact; ///this is an objective c class...

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


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.


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


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];


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消