Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua inheritance - call method on super

I'm using cocos2d-x [lua binding] to port my cocos2d [objective-c] game and I had few classes subclassed. For example I've got a subclass of CCScene [nevermind what it does], which has a onEnter method. In my subclass, KCCScene I also had onEnter and there I called [super onEnter].

So I've read quite a few docs describing inheritance in Lua, but I have never seen a call to the super method.

Is it possible to do? If it is, how should I do something like super:onEnter() ?

Thanks

like image 384
Krystian Avatar asked Jan 12 '12 13:01

Krystian


1 Answers

It really depends on the particular way on which you are doing OO. Some libs provide an "out of the box" way of doing super, others don't. I'm not familiar with Cocos2d-x, but I think it doesn't have one.

A way to achieve that is to use the superclass directly, like this:

function KCCScene:onEnter()
  doThis()
  CCScene.onEnter(self)
  doThat()
end

That should work with all libs doing OO, including Cocos2d-x. However, you lose the nice ":" syntax, and you need to include an explicit "self" parameter.

like image 185
kikito Avatar answered Nov 15 '22 09:11

kikito