How does lua object inherit from CClayer?

How does lua object inherit from CClayer?
0.0 0


I want to let lua object inherit from cocos2dx c++ class,is it possible?
I can imagine the implementation may be like the following codes.

LMap. = {}

function LMap:new
inst = {}
setmetatable(inst, self)
self.index = self
setmetatable(self, CCLayer)
CClayer:index = CClayer
return inst

if it is possible, what is the right implementation? your help would be appreciated.


cocos2d-x use tolua*+ to export lua interface, for every c*+ object exist in lua is userdata, in lua 5.1 userdata can associate a private table call “environment”, tolua++ use this feature to store data for every object. when you index the object, it will find in “environment” first. so you can inhrit like this

—define class LMap
function LMap:printf()
printf(“Lmap Printf”)
—create layer object
—let layer inherit LMap
local t=tolua.getpeer(layer) — get environment table
if not t then
t ={}
setmetable(t,LMap) — enviroment tabel inherit Lmap
tolua.setpeer(layer,t) — set environment table
—call LMap method
layer:printf() — output is : Lmap Printf

a bit boring to implement, this is the best way i find.


funA = class(“funcA”,
return CCLayer:create()

funcA.__index = funcA

i think this is what you want