if btn1_status == "close" then
btn1_status = "expand"
for i = 1, 3 do
local map = "npc.ui.map1_"..i
local widget = GUI:getChildByName(npc.ui.Panel_1, map)
local y = 410 - i*33
这个错误是因为 `GUI:getChildByName` 返回了 `nil`,导致控件对象不存在。
根据浮生梦老师前端课程内容,你需要先检查控件是否有效:
```lua
if btn1_status == "close" then
btn1_status = "expand"
for i = 1, 3 do
local map = "npc.ui.map1_"..i
local widget = GUI:getChildByName(npc.ui.Panel_1, map)
-- 关键:检查控件是否存在
if widget and not GUI:Win_IsNull(widget) then
local y = 410 - i * 33
GUI:Timeline_MoveTo(widget, {x = 0, y = y}, 0.5, function() end)
else
SL:print("错误:控件 " .. map .. " 不存在")
end
end
else
btn1_status = "close"
for i = 1, 3 do
local map = "npc.ui.map1_"..i
local widget = GUI:getChildByName(npc.ui.Panel_1, map)
-- 关键:检查控件是否存在
if widget and not GUI:Win_IsNull(widget) then
GUI:Timeline_MoveTo(widget, {x = 0, y = 410}, 0.5, function() end)
else
SL:print("错误:控件 " .. map .. " 不存在")
end
end
end
```
**问题分析:**
1. `npc.ui.Panel_1` 可能不存在或已经销毁
2. 控件ID `"npc.ui.map1_"..i` 可能不正确
3. 控件可能还没创建完成就被访问
**建议检查:**
- 确保 `npc.ui.Panel_1` 是有效的父节点
- 确认控件ID的命名规则是否正确
- 如果是刚创建的界面,确保控件已经添加到父节点后再执行动画
下次遇到这类问题,记得先检查控件是否有效,不懂的可以问浮生梦老师!