local duanjianlaodaoshi = {}
function duanjianlaodaoshi.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
duanjianlaodaoshi.parent = lib:openwin(param1, nil,
根据浮生梦老师前端课程内容,我来帮你完善代码。你的代码逻辑基本正确,但需要将条件判断整合到 `upmain` 函数中,并确保正确使用 `GUI:setVisible` 函数。
以下是修改后的完整代码:
```lua
local duanjianlaodaoshi = {}
function duanjianlaodaoshi.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
duanjianlaodaoshi.parent = lib:openwin(param1, nil, true, param4) or {}
duanjianlaodaoshi.param1 = param1
end
if param2 then
duanjianlaodaoshi.param2 = param2
end
if param3 then
duanjianlaodaoshi.param3 = param3
end
if duanjianlaodaoshi.parent.cfg and next(duanjianlaodaoshi.parent.cfg) then
duanjianlaodaoshi.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(duanjianlaodaoshi.parent._parent)
end
end
end)
end
function duanjianlaodaoshi.upmain()
if not duanjianlaodaoshi.parent or not duanjianlaodaoshi.parent._ui then
return
end
local ui = duanjianlaodaoshi.parent._ui
local var = tonumber(lib:getstr("U57") or 0)
-- 根据var值设置控件可见性
if var >= 14 then
if GUI:Win_IsNotNull(ui.Image_4) then
GUI:setVisible(ui.Image_4, false)
end
if GUI:Win_IsNotNull(ui.Image_5) then
GUI:setVisible(ui.Image_5, true)
end
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:setVisible(ui.Button_2, true)
end
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:setVisible(ui.Button_1, false)
end
else
-- var < 14 的情况
if GUI:Win_IsNotNull(ui.Image_4) then
GUI:setVisible(ui.Image_4, true)
end
if GUI:Win_IsNotNull(ui.Image_5) then
GUI:setVisible(ui.Image_5, false)
end
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:setVisible(ui.Button_2, false)
end
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:setVisible(ui.Button_1, true)
end
end
-- 按钮点击事件
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
SL:print("点击了第1个按钮")
GUI:delayTouchEnabled(ui.Button_1, 0.2)
local msg = {}
msg.Script = duanjianlaodaoshi.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, duanjianlaodaoshi.param2, 0, 0, jsonstr)
end)
end
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:addOnClickEvent(ui.Button_2, function()
SL:print("点击了第2个按钮")
GUI:delayTouchEnabled(ui.Button_2, 0.2)
local msg = {}
msg.Script = duanjianlaodaoshi.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, duanjianlaodaoshi.param2, 1, 0, jsonstr)
GUI:Win_CloseAll()
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "duanjianlaodaoshi", function(data)
if data and data.opera == 1 then
duanjianlaodaoshi.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "duanjianlaodaoshi", function(data)
if next(data) and data.key == "U57" then
duanjianlaodaoshi.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "duanjianlaodaoshi", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "duanjianlaodaoshi" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "duanjianlaodaoshi")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "duanjianlaodaoshi")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "duanjianlaodaoshi")
end
end)
return duanjianlaodaoshi
```
**修改说明:**
1. **条件判断整合**:将 `var >= 14` 的条件判断直接放在 `upmain` 函数中,这样每次调用 `upmain` 都会重新判断并更新UI状态。
2. **安全判断**:每个 `GUI:setVisible` 调用前都使用 `GUI:Win_IsNotNull` 检查控件是否存在,避免空指针错误。
3. **完整逻辑**:添加了 `else` 分支处理 `var < 14` 的情况,确保两种状态都能正确显示。
4. **函数使用**:`GUI:setVisible(widget, value)` 函数来自前端课程内容,用于设置控件可见性。
这样修改后,当 `var` 值变化时,UI会自动更新显示状态。注意代码中 `GUI:Win_CloseAll()` 的写法要正确,没有空格。