[LUA-print] [18:14:18-512] [string "scripts/GUI/GUI.lua"]:0: invalid 'cobj' in function 'lua_cocos2dx_Node_getChildByName' for i=1,#_cfg do
local Button = UILib:getChildByName(ui.Img_BG, "Button_"..i)
local Shan = UILib:getChildByN
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-20 18:15:20 |
👁️ 阅读: 7
这个错误是因为你在移除组件后,又试图使用已移除的组件作为父节点。让我们一步步分析:
```lua
for i=1,#_cfg do
local Button = UILib:getChildByName(ui.Img_BG, "Button_"..i)
local Shan = UILib:getChildByName(Button, "Shanchu_"..i)
-- 问题在这里:移除组件后,Shan对象就无效了
UILib:removeFromParent(Shan)
-- 错误:这里还在使用已移除的Shan作为父节点
local Image_13 = GUI:Image_Create(Shan, "Image_13"..i, -7, -9, "res/public/1900000678_1.png")
-- 后续代码都会失败...
end
```
**正确的做法应该是:**
```lua
for i=1,#_cfg do
local Button = GUI:getChildByName(ui.Img_BG, "Button_"..i)
-- 先检查组件是否存在
if not GUI:Win_IsNull(Button) then
local Shan = GUI:getChildByName(Button, "Shanchu_"..i)
-- 如果要重新创建,先移除旧的
if not GUI:Win_IsNull(Shan) then
GUI:removeFromParent(Shan)
end
-- 创建新的Shan节点,使用Button作为父节点
local newShan = GUI:Layout_Create(Button, "Shanchu_"..i, 0, 0, 0, 0, false)
-- 然后在新节点下创建子组件
local Image_13 = GUI:Image_Create(newShan, "Image_13"..i, -7, -9, "res/public/1900000678_1.png")
GUI:setContentSize(Image_13, 157, 246)
GUI:setIgnoreContentAdaptWithSize(Image_13, false)
GUI:setAnchorPoint(Image_13, 0.00, 0.00)
GUI:setTouchEnabled(Image_13, false)
GUI:setTag(Image_13, 0)
-- Create ItemShow_1
local ItemShow_1 = GUI:ItemShow_Create(newShan, "ItemShow_1"..i, 71, 148, {index = 1, count = 1, look = true, bgVisible = false, color = 255})
GUI:setAnchorPoint(ItemShow_1, 0.50, 0.50)
GUI:setTag(ItemShow_1, 0)
-- Create ItemShow_2
local ItemShow_2 = GUI:ItemShow_Create(newShan, "ItemShow_2"..i, 71, 47, {index = 1, count = 1, look = true, bgVisible = false, color = 255})
GUI:setAnchorPoint(ItemShow_2, 0.50, 0.50)
GUI:setTag(ItemShow_2, 0)
end
end
```
**关键点:**
1. `UILib:removeFromParent(Shan)` 移除后,`Shan`对象就无效了
2. 不能再用无效的对象作为父节点创建新组件
3. 应该先创建新的父节点,然后在其下创建子组件
4. 使用 `GUI:Win_IsNull()` 检查组件是否有效
这是常见的Lua编程错误,下次注意检查组件状态再操作哦!
这个错误是因为你在移除组件后,又试图使用已移除的组件作为父节点。让我们一步步分析:
```lua
for i=1,#_cfg do
local Button = UILib:getChildByName(ui.Img_BG, "Button_"..i)
local Shan = UILib:getChildByName(Button, "Shanchu_"..i)
-- 问题在这里:移除组件后,Shan对象就无效了
UILib:removeFromParent(Shan)
-- 错误:这里还在使用已移除的Shan作为父节点
local Image_13 = GUI:Image_Create(Shan, "Image_13"..i, -7, -9, "res/public/1900000678_1.png")
-- 后续代码都会失败...
end
```
**正确的做法应该是:**
```lua
for i=1,#_cfg do
local Button = GUI:getChildByName(ui.Img_BG, "Button_"..i)
-- 先检查组件是否存在
if not GUI:Win_IsNull(Button) then
local Shan = GUI:getChildByName(Button, "Shanchu_"..i)
-- 如果要重新创建,先移除旧的
if not GUI:Win_IsNull(Shan) then
GUI:removeFromParent(Shan)
end
-- 创建新的Shan节点,使用Button作为父节点
local newShan = GUI:Layout_Create(Button, "Shanchu_"..i, 0, 0, 0, 0, false)
-- 然后在新节点下创建子组件
local Image_13 = GUI:Image_Create(newShan, "Image_13"..i, -7, -9, "res/public/1900000678_1.png")
GUI:setContentSize(Image_13, 157, 246)
GUI:setIgnoreContentAdaptWithSize(Image_13, false)
GUI:setAnchorPoint(Image_13, 0.00, 0.00)
GUI:setTouchEnabled(Image_13, false)
GUI:setTag(Image_13, 0)
-- Create ItemShow_1
local ItemShow_1 = GUI:ItemShow_Create(newShan, "ItemShow_1"..i, 71, 148, {index = 1, count = 1, look = true, bgVisible = false, color = 255})
GUI:setAnchorPoint(ItemShow_1, 0.50, 0.50)
GUI:setTag(ItemShow_1, 0)
-- Create ItemShow_2
local ItemShow_2 = GUI:ItemShow_Create(newShan, "ItemShow_2"..i, 71, 47, {index = 1, count = 1, look = true, bgVisible = false, color = 255})
GUI:setAnchorPoint(ItemShow_2, 0.50, 0.50)
GUI:setTag(ItemShow_2, 0)
end
end
```
**关键点:**
1. `UILib:removeFromParent(Shan)` 移除后,`Shan`对象就无效了
2. 不能再用无效的对象作为父节点创建新组件
3. 应该先创建新的父节点,然后在其下创建子组件
4. 使用 `GUI:Win_IsNull()` 检查组件是否有效
这是常见的Lua编程错误,下次注意检查组件状态再操作哦!
← 返回问答库