local tunshi = {}
function tunshi.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
tunshi.parent = lib:openwin(param1, nil, true, param4) or {}
你的代码已经基本完成了UI动画和事件处理,现在需要将进度条与U55变量关联。根据浮生梦老师前端课程内容,以下是完整的修改方案:
```lua
local tunshi = {}
function tunshi.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
tunshi.parent = lib:openwin(param1, nil, true, param4) or {}
tunshi.param1 = param1
end
if param2 then
tunshi.param2 = param2
end
if param3 then
tunshi.param3 = param3
end
if tunshi.parent.cfg and next(tunshi.parent.cfg) then
tunshi.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(tunshi.parent._parent)
end
end
end)
end
function tunshi.upmain()
if not tunshi.parent or not tunshi.parent._ui then
return
end
local ui = tunshi.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U55"))
SL:Print("获取当前焚天等级 var =", var)
if var == "" then var = 0 end
var = tonumber(var) or 0
-- 设置Text_1显示
if not GUI:Win_IsNull(ui.Text_1) then
GUI:Text_setString(ui.Text_1, (var or 0).."/66次")
end
-- 设置进度条(关键修改部分)
if not GUI:Win_IsNull(ui.LoadingBar_1) then
-- 计算进度百分比(var最大值20,转换为0-100)
local percent = (var / 20) * 100
if percent > 100 then percent = 100 end
if percent < 0 then percent = 0 end
GUI:LoadingBar_setPercent(ui.LoadingBar_1, percent)
SL:Print("进度条设置百分比:", percent)
end
-- 初始化所有Text控件为不可见
local textControls = {ui.Text_2, ui.Text_3, ui.Text_4, ui.Text_5}
for _, text in ipairs(textControls) do
if not GUI:Win_IsNull(text) then
GUI:setVisible(text, false)
GUI:setOpacity(text, 0)
end
end
-- 定义动画函数
local function playTextAnimation(textWidget, delayTime)
if GUI:Win_IsNull(textWidget) then
return
end
GUI:setVisible(textWidget, true)
GUI:setOpacity(textWidget, 0)
local pos = GUI:getPosition(textWidget)
GUI:setPosition(textWidget, pos.x, pos.y - 50)
local actionSequence = GUI:ActionSequence(
GUI:ActionFadeIn(0.3),
GUI:ActionMoveBy(0.3, 0, 170),
GUI:DelayTime(delayTime or 5),
GUI:ActionFadeOut(2),
GUI:CallFunc(function()
GUI:setVisible(textWidget, false)
GUI:setPosition(textWidget, pos.x, pos.y)
end)
)
GUI:runAction(textWidget, actionSequence)
end
-- 根据条件播放不同动画
if var <= 0 then
if not GUI:Win_IsNull(ui.Text_2) then
playTextAnimation(ui.Text_2, 1)
end
elseif var >= 3 then
if not GUI:Win_IsNull(ui.Text_5) then
playTextAnimation(ui.Text_5, 1)
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 = tunshi.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, tunshi.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 = tunshi.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, tunshi.param2, 1, 0, jsonstr)
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "tunshi", function(data)
if data and data.opera == 1 then
tunshi.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "tunshi", function(data)
if next(data) and data.key == "U55" then
tunshi.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "tunshi", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "tunshi" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "tunshi")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "tunshi")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "tunshi")
end
end)
return tunshi
```
**主要修改说明:**
1. **进度条关联逻辑**:
- 使用 `GUI:LoadingBar_setPercent(widget, value)` 函数设置进度条
- 将U55变量值(0-20)转换为百分比(0-100)
- 添加了边界检查,确保百分比在0-100范围内
2. **函数参数说明**:
- `GUI:LoadingBar_setPercent(widget, value)`
- `widget`: 进度条控件对象(必选)
- `value`: 进度百分比,0-100(必选)
3. **触发时机**:
- 界面打开时自动更新进度条
- U55值变化时通过事件自动更新
- 装备变化时也会触发更新
这样你的进度条就能正确显示U55变量的进度了,当U55=0时进度为0%,U55=20时进度为100%。记得检查你的UI中确实有名为`LoadingBar_1`的进度条控件。