local GUI = GUI
SL:print("GUI加载成功")
function GUI:npcUi(id)
SL:Print("=====================",id)
if not id or type(id) ~= "string" then
SL:Print("[ERROR] readUI: 路径需要为字符串")
return nil
end
local path1 = "GUIExport/1_Ex
```lua
local GUI = GUI
SL:print("GUI加载成功")
function GUI:npcUi(id)
SL:Print("=====================", id)
if not id or type(id) ~= "string" then
SL:Print("[ERROR] readUI: 路径需要为字符串")
return nil
end
local path1 = "GUIExport/1_Export_npc/" .. id .. ".lua"
local path2 = "1_Export_npc/" .. id .. ".lua"
if not SL:IsFileExist(path1) then
SL:Print("[ERROR] readUI: 文件不存在,检查文件:", path1)
return nil
else
SL:Print("[INFO] readUI: 加载成功!:", path1)
end
local _V = function(...) return SL:GetMetaValue(...) end
local parent = GUI:Win_Create(id, _V("SCREEN_WIDTH") * 0.5, _V("SCREEN_HEIGHT") * 0.5, 0, 0, false, true, true, true, true, false)
-- 使用GUI:Win_IsNull检查控件是否为空
if not parent or GUI:Win_IsNull(parent) then
SL:Print("[ERROR] readUI: 对象不能为空")
return nil
end
GUI:removeAllChildren(parent)
-- 先确保ui.bg存在再设置拖拽
GUI:LoadExport(parent, path2)
local ui = GUI:ui_delegate(parent)
-- 检查ui.bg是否存在
if ui and ui.bg and not GUI:Win_IsNull(ui.bg) then
GUI:Win_SetDrag(parent, ui.bg)
else
SL:Print("[WARNING] readUI: 未找到bg节点,无法设置拖拽")
end
return ui
end
return GUI
local npc = {}
SL:print("NPC加载成功")
npc.cfg = {}
function npc:main()
SL:print("进来NPC;")
-- 错误:调用GUI:npcUi("E_movenpc")但没有接收返回值
-- 正确应该接收返回值或删除这行
-- GUI:npcUi("E_movenpc") -- 这行会返回nil但没有接收
self.ui = GUI:npcUi("E_move_npc")
-- 需要先检查self.ui是否存在
if self.ui and self.ui.colse and not GUI:Win_IsNull(self.ui.colse) then
GUI:addOnClickEvent(self.ui.colse, function()
SL:print("点击了关闭按钮")
end)
else
SL:Print("[ERROR] npc:main: 未找到colse按钮或UI加载失败")
end
end
return npc
```
**问题分析:**
1. **第16行错误**:在`L_movenpc.lua`第16行,你尝试访问一个不存在的索引。根据代码分析,最可能是:
- `self.ui.colse`为nil(拼写错误,应该是`close`)
- `GUI:npcUi("E_move_npc")`返回nil导致`self.ui`为nil
2. **主要问题**:
- 没有使用`GUI:Win_IsNull()`检查控件是否为空
- 调用`GUI:npcUi("E_movenpc")`但没有接收返回值,浪费了一次加载
- 拼写错误:`colse`应该是`close`
- 在`ui_delegate`之前就尝试访问`parent.bg`(父节点还没有子控件)
3. **修复要点**:
- 所有GUI控件使用前都要用`GUI:Win_IsNull()`检查
- 确保路径正确,文件存在
- 先加载UI再访问子节点
- 注意变量拼写
下次写代码要仔细检查这些细节哦,不懂的可以问浮生梦尊师!