How to implement objective-c 's delegates ?

How to implement objective-c 's delegates ?
0.0 0



I ’m in the process of migrating my game from cocos2d-iphone to cocos2d-x, so I may have a few questions these days :slight_smile:
I was wondering what’s the best way to implement delegates in cocos2d-x ?
In objective-C I have a navigation layer with a delegate like this :

@protocol NavigationDelegate
- (void)back;
- (void)forth;

@interface NavigationLayer : CCLayer
    id delegate;

And other layers implementation looks like :

@interface MyScene : CCLayer
[navigationLayer setDelegate:self];

- (void)back { ... }
- (void)forth { ... }

How can I convert this code to cocos2d-x and c++ ?

Thanks in advance


You can refer to the implementation of CCTouchDelegate.


Thanks Walzer !


blue eur wrote:

Thanks Walzer !

I hate to be dense, but every time I try and declare a CCLayer subclass as CCListViewDelegate as shown in CCLayer with CCTouchDelegate (or any other delegate) the compiler complains at LAYER_CREATE_FUNC(MyLayerClass) saying “Allocating an object of abstract class ‘MyClassName’”. blue eur or anybody else, any chance I can beg a snippet of code as an example?


Hi Chris,

Did you implement the delegate methods in your class ?
Something like that :

class MyDelegate
    MyDelegate() {};
    virtual ~MyDelegate() {};

    virtual void method1() {};
    virtual void method2() {};

class MyScene : public CCScene, public MyDelegate
    virtual void method1() {};
    virtual void method2() {};