使用如下方式加载cocosstudio导出的场景, 然后获取Text控件之后, 再一个回调函数中setString 提示报错 invalid ‘cobj’ in function ‘lua_cocos2dx_ui_Text_setString’
代码如下:
local SceneNode = cc.uiloader:load(“LoginScene.csb”)
:addTo(self)
:setContentSize(size)
ccui.Helper:doLayout(SceneNode)
local loginStatus = cc.uiloader:seekNodeByName(SceneNode, "loginStatus")
if not loginStatus then
error("error loginStatus not find in LoginScene.csb")
else
self.loginStatus = loginStatus
end
local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
local customListener = cc.EventListenerCustom:create("TEST",
function(event)
loginStatus:setString("已登陆")
end)
eventDispatcher:addEventListenerWithFixedPriority(customListener, 1)
有的时候是可以的 ,有的时候会报错。 使用的是quicklua 3.5 cocosstudio版本是2.1.5 (是不是因为从cocosstudio 中导出的是Scene, 而不是层或者 节点?)
并且从SceneNOde 中 使用cc.uiloader:seekNodeByName(SceneNode, “btn”) 查找btn 也会提示失败
如下函数并且做了修改:
function uiloader:seekNodeByName(parent, name)
if not parent then
return
end
if name == parent:getName() then
return parent
end
local findNode
local children = parent:getChildren()
local childCount = parent:getChildrenCount()
if childCount < 1 then
return
end
for i=1, childCount do
if "table" == type(children) then
parent = children*
elseif "userdata" == type(children) then
parent = children:objectAtIndex(i - 1)
end
if parent then
if name == parent:getName() then
return parent
end
end
end
for i=1, childCount do
if "table" == type(children) then
parent = children*
elseif "userdata" == type(children) then
parent = children:objectAtIndex(i - 1)
end
if parent then
findNode = self:seekNodeByName(parent, name)
if findNode then
return findNode
end
end
end
return
end
**