---@diagnostic disable: undefined-global
local cfg = require("GUILayout.Npc配置.人物转生配置")
local ui = {}
function ui.init(parent)
local reincarnation_level = SL:Get_RELEVEL() --玩家转生等级
local next = cfg[reincarnation_level + 1] or cfg[0]
local width = S
根据浮生梦老师前端课程内容,你的代码存在几个关键问题:
1. **事件名错误**:应该是 `LUA_EVENT_REINLEVELCHANGE` 而不是 `LUA_EVENT_REINLEVEL_CHANGE`
2. **重新调用main会导致界面重复创建**:每次事件触发都会创建新界面,但旧界面不会自动关闭
正确的更新方式应该是:
```lua
-- 保存当前界面引用
local currentWindow = nil
local function updateReincarnationUI()
-- 如果已有界面,先更新界面内容而不是重新创建
if currentWindow and not GUI:Win_IsNull(currentWindow) then
local reincarnation_level = SL:Get_RELEVEL()
local ui = GUI:ui_delegate(currentWindow)
-- 更新转生等级显示
if ui.Text_1 then
GUI:Text_setString(ui.Text_1, tostring(reincarnation_level))
end
-- 更新属性显示
local cfg = require("GUILayout.Npc配置.人物转生配置")
local next = cfg[reincarnation_level + 1] or cfg[0]
if ui.Text_2 then
GUI:Text_setString(ui.Text_2, "血量增加:" .. (next.hp or ""))
end
if ui.Text_3 then
GUI:Text_setString(ui.Text_3, "蓝量增加:" .. (next.mp or ""))
end
-- 更新道具显示
if ui.Item_1 then
GUI:ItemShow_setData(ui.Item_1, { index = 2, look = true, count = next.money or 0, bgVisible = true })
end
if ui.Item_2 then
GUI:ItemShow_setData(ui.Item_2, { index = 10421, look = true, count = next.stone or 0, bgVisible = true })
end
end
end
local function main()
-- 如果已有界面,直接更新
if currentWindow and not GUI:Win_IsNull(currentWindow) then
updateReincarnationUI()
return currentWindow
end
-- 创建界面
currentWindow = GUI:Win_Create("Layout", 0, 0, 0, 0)
if GUI:Win_IsNull(currentWindow) then
SL:Print("Error: Failed to create window")
return
end
GUI:LoadExport(currentWindow, "4")
local ui = GUI:ui_delegate(currentWindow)
GUI:Win_SetDrag(currentWindow, ui.ImageView)
-- 添加关闭按钮事件
GUI:addOnClickEvent(ui.Button_1, function()
GUI:Win_Close(currentWindow)
currentWindow = nil
local module = SL:Require("dev.GUILayout.TopBut")
if module and module.main then
module.main()
end
end)
-- 开始转生按钮
GUI:addOnClickEvent(ui.Button_2, function()
SL:SendLuaNetMsg(4, 1, 0, 0, "开始转生")
end)
-- 初始更新
updateReincarnationUI()
return currentWindow
end
-- 正确的事件名
SL:RegisterLUAEvent(LUA_EVENT_REINLEVELCHANGE, "转生等级改变", function()
updateReincarnationUI()
end)
return { main = main }
```
**核心改进:**
1. 使用 `currentWindow` 变量保存界面引用
2. 事件触发时调用 `updateReincarnationUI()` 只更新内容,不重新创建
3. 关闭界面时将 `currentWindow` 设为 nil
4. 使用正确的转生等级改变事件名
这样转生等级改变时,界面内容会自动更新,不会重复创建新界面。不懂的可以问浮生梦尊师!