Subclassing Cocos2d-x classes

Subclassing Cocos2d-x classes
0.0 0


Is it possible to sub-class Cocos2d-x classes within Lua at the moment? A lot of things I keep needing to do require overriding of the virtual methods (for example ‘paint’) I followed the instructions here: and rebuilt the lua bindings with it integrated but I ran into problems with eht existing .pkg files (most of which I fixed by moving the virtual methods into private: accessor sections) but I could not get the touchdelegates to compile properly afterwards :frowning:

i) Is there a simpler way to achieve what I want ?
ii) Has anyone else got virtual_method hooks to work with the tolua bindings ?



I have the same problem. does anybody help us ?


I got bored so I did some work on this, you can find a very experimental approach to the problem here:

Unfortunately you cannot currently override init (and even if you could I can’t quite get my head around the memory management side-effects, but I’ve modified ‘HelloLua’ slightly to demonstrate overriding of ‘onEnter’ on the FarmLayer’s CCLayer instance.

Interesting ?


Seriously? No one’s interested in this work ? :frowning: Bad times.


Hi Ciaran:

I am reading your patch on Github. It is great and helpful. I hope cocos2dx guys could merge your patch to master. It is really helpful to develop game with Lua.

Thanks Ciaran. i will take care of your work.



Which pull request?




I’ve not submitted a pull request. It isn’t ready for that, was only going to bother if people had reviewed it and thought it was worth progressing with ?


Hi Ciaran,

I know it’s been a while…
I went through your changes, and I have to admit - wow. That’s a lot of work! Thanks for that.
Unfortunately my knowledge of C/C++ is… well long gone I would say [hence using Lua to port my cocos2d game] and I don’t get most of it :slight_smile:
I assume everything in there is fine and I would really like to use it, however I’ve got a question.

When you override a function, how can one call [super onEnter] ?
I do remember that when I extended CCNode and forgot to call super onEnter I ended up with no touch handling at all :slight_smile:

I hope you still follow this thread and still have some energy left to answer my q :wink:

I’m just wondering… can’t I use approach described in here:
to handle inheritance, function overriding and using “super” ?


Hmmm, I think it would be something along the lines of


But I’m not 100% , I wrote this a while ago, and it seemed nobody was interested, so I moved onto other things :slight_smile:


Ah yeah… I understand :slight_smile:
I just moved from cocos2d and starting to create a skeleton I will use.
I just stumbled upon the onEnter and onExit implementation in many of my classes :slight_smile:

Anyway… thanks :slight_smile: