你的代码有几个问题,我帮你分析一下:
**主要问题:**
1. **获取子控件失败**:`GUI:getChildByName(npc.ui.Panel_1, "map1_"..i)` 返回了nil,说明这些控件不存在
2. **坐标计算逻辑错误**:在展开/收起时,坐标计算有问题
**修改建议:**
```lua
SL:print("打开每日活动界面")
local npc = {}
local function networkCB(msgID, p1, p2, p3, msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
SL:print("参数3: " .. tostring(p3))
SL:print("字符串: " .. tostring(msgData))
SL:print("=======================")
if msgID == 1019 and msgData == "每日活动" then
if p3 == 0 then
npc:openWin()
end
end
end
function npc:openWin()
-- 创建界面
local win_dailyAct = GUI:Win_Create("win_dailyAct", 0, 0, 0, 0)
local btn1_status = "close"
local btn2_status = "close"
GUI:LoadExport(win_dailyAct, "dailyAct")
npc.ui = GUI:ui_delegate(win_dailyAct)
-- 先检查控件是否存在
SL:print("检查Panel_1是否存在:", npc.ui.Panel_1)
GUI:addOnClickEvent(npc.ui.esc, function()
GUI:Win_Close(win_dailyAct)
end)
GUI:addOnClickEvent(npc.ui.btn_1main, function()
-- 先获取所有子控件
local map1_btns = {}
local map2_btns = {}
for i = 1, 3 do
map1_btns[i] = GUI:getChildByName(npc.ui.Panel_1, "map1_"..i)
map2_btns[i] = GUI:getChildByName(npc.ui.Panel_1, "map2_"..i)
-- 打印调试信息
SL:print("map1_"..i.."是否存在:", map1_btns[i] ~= nil)
SL:print("map2_"..i.."是否存在:", map2_btns[i] ~= nil)
end
if btn1_status == "close" then
btn1_status = "expand"
for i = 1, 3 do
if map1_btns[i] then
local y1Pos = 113 - i * 35
GUI:Timeline_MoveTo(map1_btns[i], {x = 0, y = y1Pos}, 0.3)
end
end
-- 移动btn_2main
if npc.ui.btn_2main then
local yPos_btn = 113 - 3 * 35
GUI:Timeline_MoveTo(npc.ui.btn_2main, {x = 0, y = yPos_btn}, 0.3)
end
-- 移动map2按钮
for i = 1, 3 do
if map2_btns[i] then
local yPos_btn = 113 - 3 * 35
GUI:Timeline_MoveTo(map2_btns[i], {x = 0, y = yPos_btn}, 0.3)
end
end
else
btn1_status = "close"
for i = 1, 3 do
if map1_btns[i] then
GUI:Timeline_MoveTo(map1_btns[i], {x = 0, y = 113}, 0.3)
end
end
-- 恢复btn_2main位置
if npc.ui.btn_2main then
GUI:Timeline_MoveTo(npc.ui.btn_2main, {x = 0, y = 164}, 0.3)
end
-- 恢复map2按钮位置
for i = 1, 3 do
if map2_btns[i] then
GUI:Timeline_MoveTo(map2_btns[i], {x = 0, y = 164}, 0.3)
end
end
end
end)
end
SL:RegisterLuaNetMsg(1019, networkCB)
```
**需要检查的地方:**
1. **确认dailyAct.layout文件**中确实有`map1_1`、`map1_2`、`map1_3`这些控件
2. **确认控件在Panel_1下面**,而不是在其他父节点下
3. **使用GUI:getChildByName**时要注意大小写和拼写
如果还是找不到控件,可以先用`SL:print`输出`npc.ui`的所有键值,看看实际有哪些控件可用。