cocos2d-x + lua 自定义类成员调用问题

我在MainPageLayer.lua 中定义自己的类 实现如下

MainPageLayer = class("MainPageLayer")
MainPageLayer.__index = MainPageLayer
function MainPageLayer:init()
-- do my init
end
function MainPageLayer:run()
-- do my init
end
function MainPageLayer.create()
    local layer = CCLayer:create() 
    local t = tolua.getpeer(layer)
    if not t then
        t = {}
        tolua.setpeer(layer, t)
    end
    setmetatable(t, MainPageLayer) 
    if nil ~= layer then
        layer:init()
    end
    return layer  
end

其中class() 方法是借用 testLua Demo中的 extern.lua 中的方法
在另一文件.lua 中某一函数中

local mainPageLayer = MainPageLayer.create()
print("MainPageLayer.create() type ..."..type(mainPageLayer)) -- 打出的是userdata
mainPageLayer:setTag(nTagMainPageLayer) -- nTagMainPageLayer  = 10000

然后加到层中
– mainPageLayer:run() – 此时若调用run()不报错

在此文件的另一函数

local child = mainMenuMultiplexLayer:getChildByTag(nTagMainPageLayer)print("child type ..."..type(child))  -- 打出的是userdata
print ("child tag .."..child:getTag()) -- 打出的是10000
if nil ~= child then
    local mainPage = tolua.cast(child, "MainPageLayer")
    print("mainPage type ..."..type(mainPage)  -- 打出的是string 类型 
    mainPage:run() --调用成员函数  -- error: 调用成员函数 是nil .
end

mainPage 的类型为什么是 string 不应该和MainPageLayer.create() 一样是 userdata 类型吗
成员类函数也不能调用 了
是我自定义类有问题吗?

你直接调用child:run()不就得了 还强转什么?在说你强转也不对啊 MainPageLayer本来就是个table 当然是String了

是我想多了, 刚用lua , 还不太了解, local mainPage = tolua.cast(child, “MainPageLayer”) 这里MainPageLayer就是我自定义的表而非在C++中自定义的类,So 。。

tolua.getpeer这玩意有什么用啊?我就用了class,其它都没用过啊,求解!!!

遇到同样的问题,getpeer是干嘛用的?