local moshen = {}
function moshen.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
moshen.parent = lib:openwin(param1, nil, true, param4) or {}
你的问题在于界面重新打开时,`moshen.parent` 可能被重置了。在 `main` 函数中,你通过 `lib:openwin` 创建了 `moshen.parent`,但当你关闭界面后再次调用 `main` 时,之前的 `moshen.parent` 可能已经被清理或覆盖。
关键问题在于:
1. 当界面关闭时,`moshen.parent` 可能没有被正确清理或重置。
2. 重新调用 `main` 时,`moshen.parent` 可能指向了一个无效的界面对象。
解决方案:在界面关闭时,显式地将 `moshen.parent` 设置为 `nil`,并在 `main` 中确保每次打开都是全新的初始化。
修改你的 `LUA_EVENT_CLOSEWIN` 事件处理:
```lua
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshen", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "moshen" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "moshen")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "moshen")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshen")
-- 新增:清理父节点引用
moshen.parent = nil
moshen.param1 = nil
moshen.param2 = nil
moshen.param3 = nil
end
end)
```
同时,在 `main` 函数开始时,可以添加一个安全判断:
```lua
function moshen.main(param1, param2, param3, param4)
-- 如果已经有打开的界面,先关闭它(可选)
if moshen.parent and GUI:Win_IsNotNull(moshen.parent._parent) then
GUI:Win_Close(moshen.parent._parent)
end
lib:Require(param1, function(state)
if state then
if param1 then
moshen.parent = lib:openwin(param1, nil, true, param4) or {}
moshen.param1 = param1
end
if param2 then
moshen.param2 = param2
end
if param3 then
moshen.param3 = param3
end
if moshen.parent.cfg and next(moshen.parent.cfg) then
moshen.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(moshen.parent._parent)
end
end
end)
end
```
这样修改后,每次重新打开界面都会重新初始化 `moshen.parent`,避免引用到旧的、已关闭的界面对象。