local leishen = {}
local function showEffect(msgID, p1, p2, p3, msgData)
local data = SL:JsonDecode(msgData, false) or {}
SL:Print("特效数据", data)
local num = data.num or 0
local eff1, eff2 = data.preEff, data.startEff
-- 创建父节
从你的代码来看,点击按钮后特效持续时间很短的问题主要出在定时器的使用上。你使用了 `SL:ScheduleOnce` 来创建循环定时器,这是不正确的。
**问题分析:**
1. `SL:ScheduleOnce` 是单次延迟执行,只执行一次
2. 你试图用它来实现循环更新进度条,但每次都是重新创建单次定时器
3. 应该使用 `SL:Schedule` 来实现循环定时器
**修正后的定时器代码:**
```lua
if GUI:Win_IsNotNull(ui["Button_5"]) then
GUI:addOnClickEvent(ui["Button_5"], function()
SL:Print("点击Button_5触发特效")
GUI:delayTouchEnabled(ui["Button_5"], 0.2)
GUI:Win_CloseAll()
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
local effectData = leishen.createButtonEffectData()
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
for i = 1, effectData.num do
effectData.mapdata[i] = {
x = math.random(100, screenWidth - 100),
y = math.random(100, screenHeight - 100)
}
end
showEffect(2027, 0, 0, 0, SL:JsonEncode(effectData))
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 4, 0, jsonstr)
-- 创建图片
local image6 = GUI:Image_Create(sceneLayer, "Image_6", 100, 100, "res/erdalu/leishen/jdt/leishen.png")
if GUI:Win_IsNotNull(image6) then
SL:Print("Image_6创建成功")
GUI:setVisible(image6, true)
else
SL:Print("Image_6创建失败")
end
local loadingBar = GUI:LoadingBar_Create(image6, "LoadingBar_1", 100, 100, "res/erdalu/leishen/jdt/1.png", 0)
GUI:LoadingBar_setPercent(loadingBar, 100)
local count = 40
local loadingTimer = nil
-- 使用SL:Schedule创建循环定时器
loadingTimer = SL:Schedule(function()
count = count - 1
if count >= 0 then
local percent = (count / 40) * 100
if not GUI:Win_IsNull(loadingBar) then
GUI:LoadingBar_setPercent(loadingBar, percent)
end
if count == 0 then
if not GUI:Win_IsNull(image6) then
GUI:setVisible(image6, false)
end
if not GUI:Win_IsNull(loadingBar) then
GUI:LoadingBar_setPercent(loadingBar, 100)
end
if not GUI:Win_IsNull(sceneLayer) then
GUI:removeFromParent(sceneLayer)
end
-- 停止定时器
return false
end
else
-- 停止定时器
return false
end
return true
end, 1)
end)
end
```
**主要修改点:**
1. 将 `SL:ScheduleOnce` 改为 `SL:Schedule`
2. `SL:Schedule` 的回调函数返回 `true` 表示继续执行,返回 `false` 表示停止
3. 当倒计时到0时,清理所有控件并停止定时器
这样修改后,进度条就会每秒更新一次,持续40秒,而不是只执行零点几秒就结束了。