local rongyuchenghao = {}
function rongyuchenghao.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
rongyuchenghao.parent = lib:openwin(param1, nil, true, pa
我看了一下你的代码,整体结构没问题,但有几个关键问题需要修正:
## 主要问题:
### 1. **list_layout 变量名错误**
```lua
local list_layout = ui.Image_layout -- 这个可能不存在
```
根据浮生梦老师前端课程内容,如果你的容器是ListView或其他布局组件,应该用正确的名称。比如:
```lua
local list_layout = ui.ListView_1 -- 或者实际的ListView名称
```
### 2. **移动逻辑的重复执行问题**
```lua
if var >= 8 and not rongyuchenghao.moved_8 then
```
这个逻辑只会在第一次达到8级时执行,如果玩家从8级降到7级再升到8级,就不会再执行了。建议改成:
```lua
-- 8级移动逻辑
if var >= 8 then
for i = 1, 8 do
local img = ui["Image_"..i]
local parent = GUI:getParent(img)
if img and not GUI:Win_IsNull(img) and parent ~= list_layout then
GUI:removeFromParent(img)
GUI:ListView_pushBackCustomItem(list_layout, img)
end
end
end
```
### 3. **关闭事件处理不完整**
```lua
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "rongyuchenghao", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "rongyuchenghao" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "rongyuchenghao")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "rongyuchenghao")
end
end)
```
缺少了对 `LUA_EVENT_SERVER_VALUE_CHANGE` 事件的注销。
## 修正后的完整代码:
```lua
local rongyuchenghao = {}
function rongyuchenghao.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
rongyuchenghao.parent = lib:openwin(param1, nil, true, param4) or {}
rongyuchenghao.param1 = param1
end
if param2 then
rongyuchenghao.param2 = param2
end
if param3 then
rongyuchenghao.param3 = param3
end
if rongyuchenghao.parent.cfg and next(rongyuchenghao.parent.cfg) then
rongyuchenghao.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(rongyuchenghao.parent._parent)
end
end
end)
end
local function create_attr(cur_data, next_data)
local ui = rongyuchenghao.parent._ui
lib:create_attr_list(ui.attr_layout, cur_data, next_data, true, 12)
end
function rongyuchenghao.upmain()
if not rongyuchenghao.parent or not rongyuchenghao.parent._ui then
return
end
local var = tonumber(lib:getstr("U12") or 0) or 0
local ui = rongyuchenghao.parent._ui
local level = var
local next_level = var + 1
local cur_data = rongyuchenghao.parent.cfg[level] or {}
local next_data = rongyuchenghao.parent.cfg[next_level] or {}
local next_cfg = rongyuchenghao.parent.cfg[next_level]
if not next_cfg then return end
-- 显示隐藏逻辑
for i = 23, 44 do
local imgName = "Image_" .. i
local imgWidget = ui[imgName]
if imgWidget then
local targetLevel = i - 22
GUI:setVisible(imgWidget, var >= targetLevel)
end
end
for i = 1, 22 do
local textName = "Text_" .. i
local textWidget = ui[textName]
if textWidget then
GUI:setVisible(textWidget, true)
GUI:Text_setTextColor(textWidget, var >= i and "#c0c0c0" or "#808080")
end
end
-- ==============================================
-- 【修正】核心功能:8级 / 16级 移动分组
-- ==============================================
-- 注意:这里需要你确认实际的容器名称
local list_layout = ui.ListView_1 -- 修改为实际的ListView名称
if not GUI:Win_IsNull(list_layout) then
-- 8级:把 1-8 移到末尾
if var >= 8 then
for i = 1, 8 do
local img = ui["Image_"..i]
if img and not GUI:Win_IsNull(img) then
local current_parent = GUI:getParent(img)
if current_parent ~= list_layout then
GUI:removeFromParent(img)
GUI:ListView_pushBackCustomItem(list_layout, img)
end
end
end
end
-- 16级:把 9-16 移到末尾
if var >= 16 then
for i = 9, 16 do
local img = ui["Image_"..i]
if img and not GUI:Win_IsNull(img) then
local current_parent = GUI:getParent(img)
if current_parent ~= list_layout then
GUI:removeFromParent(img)
GUI:ListView_pushBackCustomItem(list_layout, img)
end
end
end
end
end
create_attr(cur_data, next_data)
lib:createitemlist(ui.item_layout, next_cfg, 10)
-- 绿点
if lib:checkitem(next_cfg, true) then
rongyuchenghao.btn_green_frames = lib:create_green_point(ui["Button_1"], 108, 6)
else
lib:remove_widget(rongyuchenghao.btn_green_frames)
end
GUI:addOnClickEvent(ui["Button_1"], function()
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
local msg = { Script = rongyuchenghao.param1 }
SL:SendLuaNetMsg(58003, rongyuchenghao.param2, 0, 0, SL:JsonEncode(msg))
end)
end
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "rongyuchenghao", function(data)
if data and data.opera == 1 then
rongyuchenghao.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "rongyuchenghao", function(data)
if next(data) and data.key == "U12" then
rongyuchenghao.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "rongyuchenghao", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "rongyuchenghao" then
-- 注销所有事件
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "rongyuchenghao")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "rongyuchenghao")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "rongyuchenghao")
-- 清理变量
rongyuchenghao.parent = nil
rongyuchenghao.param1 = nil
rongyuchenghao.param2 = nil
rongyuchenghao.param3 = nil
rongyuchenghao.moved_8 = nil
rongyuchenghao.moved_16 = nil
end
end)
return rongyuchenghao
```
## 需要你确认的地方:
1. **第47行**:`local list_layout = ui.ListView_1` - 请确认你的界面中ListView的实际名称
2. **第51-73行**:移除了 `moved_8` 和 `moved_16` 标记,改为每次都会检查并移动,这样更稳定
3. **第98-103行**:在关闭事件中增加了清理逻辑,避免内存泄漏
如果你还有不明白的地方,可以继续问我!记得检查一下你的界面布局,确保容器组件名称正确。