我在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
<p> </p>
<p>
其中class() 方法是借用 testLua Demo中的 extern.lua 中的方法</p>
<p>在另一文件.lua 中某一函数中</p>
<p></p>
<pre class="" "brush:lua;""="" toolbar:="" ""="" true;="" auto-links:="" false;"="">local mainPageLayer = MainPageLayer.create()
print("MainPageLayer.create() type ..."..type(mainPageLayer)) -- 打出的是userdata
mainPageLayer:setTag(nTagMainPageLayer) -- nTagMainPageLayer = 10000
然后加到层中
-- mainPageLayer: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 类型吗
成员类函数也不能调用 了
是我自定义类有问题吗?
可如用我在创建的时候调用自定义的成员函数并不会出错 ?
求大神帮分析下 指点可能的错误 。 谢谢!