根据你提供的错误日志和代码,问题出在窗口创建和关闭的逻辑上。让我帮你分析一下:
## 主要问题分析:
1. **窗口重复创建**:日志显示 `GUI:Win_Create, Win exist, close it TuChengPaoKu`,说明窗口已经存在但又在尝试创建
2. **无效的窗口关闭**:`GUI:Win_Close, is valid Win` 错误表明在关闭一个无效的窗口对象
## 问题代码位置:
在你的 `initUI()` 函数中,虽然你注释掉了4行代码,但问题可能出现在其他地方:
```lua
function TuChengPaoKu:initUI()
if self._inited and self.ui then
self:updateUI()
return
end
if not self.parentUI or not self.parentUI.Panel_Right then
return
end
-- 这里有问题:UIManager:winCreate 内部可能调用了 GUI:Win_Create
UIManager:winCreate(self, true, 2)
-- ... 其他代码
end
```
## 解决方案:
根据浮生梦老师前端课程内容,应该这样修改:
```lua
function TuChengPaoKu:initUI()
-- 先检查窗口是否已经存在且有效
if self.parent and not GUI:Win_IsNull(self.parent) then
self:updateUI()
return
end
if not self.parentUI or not self.parentUI.Panel_Right then
return
end
-- 确保之前的窗口被正确清理
if self.parent then
GUI:removeFromParent(self.parent)
self.parent = nil
end
-- 创建新窗口
self.parent = GUI:Win_Create("TuChengPaoKu", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 加载UI
GUI:LoadExport(self.parent, "你的UI路径")
self.ui = GUI:ui_delegate(self.parent)
-- 添加到父节点
GUI:addChild(self.parentUI.Panel_Right, self.parent)
self._inited = true
self:updateUI()
end
```
## 关键点:
1. **使用 `GUI:Win_IsNull()`** 检查窗口是否有效
2. **使用 `GUI:removeFromParent()`** 而不是直接关闭
3. **确保窗口名称唯一**,避免重复创建
4. **在创建前清理旧窗口**,避免内存泄漏
## 修改建议:
在你的 `closeUI()` 函数中也要做相应修改:
```lua
function TuChengPaoKu:closeUI()
self._inited = false
if self.parent and not GUI:Win_IsNull(self.parent) then
GUI:removeFromParent(self.parent)
end
self.parent = nil
self.ui = nil
end
```
这样修改后,应该能解决窗口创建和关闭的冲突问题。记得检查你的UI路径是否正确配置!