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))
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-27 00:43:27 |
👁️ 阅读: 6
你的代码有几个问题需要修正:
1. **面板变量引用错误**:`panel = "npc.ui.Panel_" .. x` 创建的是字符串,而不是控件对象
2. **x变量作用域问题**:函数内部的 `local x` 覆盖了外部变量
3. **动画回调函数使用错误**:`GUI:ActionEaseExponentialOut` 不是这样用的
修正后的代码:
```lua
SL:print("打开沙城捐献界面")
local npc = {}
local currentPanel = 1 -- 当前显示的面板索引
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 == 1017 and msgData == "沙城捐献" then
if p3 == 0 then
npc:openWin()
end
end
end
function npc:openWin()
-- 创建外部界面
local win_donate = GUI:Win_Create("win_donate", 0, 0, 0, 0)
GUI:LoadExport(win_donate, "donate")
npc.ui = GUI:ui_delegate(win_donate)
GUI:addOnClickEvent(npc.ui.esc, function()
GUI:Win_Close(win_donate)
end)
-- 按钮1点击事件
GUI:addOnClickEvent(npc.ui.Button_1, function()
-- 隐藏当前面板
local currentPanelName = "Panel_" .. currentPanel
if npc.ui[currentPanelName] then
GUI:setVisible(npc.ui[currentPanelName], false)
end
-- 计算下一个面板索引
currentPanel = (currentPanel + 1) % 3
if currentPanel == 0 then
currentPanel = 3
end
-- 显示新面板并添加动画
local newPanelName = "Panel_" .. currentPanel
if npc.ui[newPanelName] then
GUI:setVisible(npc.ui[newPanelName], true)
GUI:Timeline_MoveTo(npc.ui[newPanelName], {x = 5, y = 0}, 0.35)
end
end)
-- 按钮2点击事件
GUI:addOnClickEvent(npc.ui.Button_2, function()
-- 隐藏当前面板
local currentPanelName = "Panel_" .. currentPanel
if npc.ui[currentPanelName] then
GUI:setVisible(npc.ui[currentPanelName], false)
end
-- 计算前一个面板索引
currentPanel = currentPanel - 1
if currentPanel < 1 then
currentPanel = 3
end
-- 显示新面板并添加动画
local newPanelName = "Panel_" .. currentPanel
if npc.ui[newPanelName] then
GUI:setVisible(npc.ui[newPanelName], true)
GUI:Timeline_MoveTo(npc.ui[newPanelName], {x = 5, y = 0}, 0.35)
end
end)
end
SL:RegisterLuaNetMsg(1017, networkCB)
```
**主要修改点:**
1. 使用 `currentPanel` 全局变量记录当前显示的面板索引
2. 通过 `npc.ui["Panel_" .. index]` 正确获取控件对象
3. 移除了错误的 `GUI:ActionEaseExponentialOut` 调用
4. 添加了面板显示/隐藏逻辑
下次注意变量作用域和对象引用的正确写法,有问题可以再问浮生梦老师!
← 返回问答库