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:Schedule` 来创建循环定时器,但根据浮生梦老师前端课程内容,`SL:Schedule` 是用于帧率相关的高频更新,而你需要的是按秒计时的定时器。
在你的代码中,`SL:Schedule(function() ... end, 1)` 这里的1表示每帧执行,而不是每秒执行。所以40次很快就执行完了。
**正确的做法是使用 `SL:ScheduleOnce` 创建单次延迟,或者使用 `GUI:schedule` 创建循环定时器:**
```lua
-- 方法1:使用GUI:schedule创建循环定时器(推荐)
loadingTimer = GUI:schedule(loadingBar, 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 -- 返回false停止定时器
end
else
return false -- 返回false停止定时器
end
return true -- 返回true继续执行
end, 1) -- 这里1表示1秒执行一次
```
**或者使用 `SL:ScheduleOnce` 配合递归:**
```lua
local function updateLoadingBar()
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
-- 递归调用,1秒后再次执行
SL:ScheduleOnce(updateLoadingBar, 1)
else
-- 倒计时结束
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
end
end
end
-- 启动倒计时
SL:ScheduleOnce(updateLoadingBar, 1)
```
**关键点说明:**
1. `GUI:schedule(widget, callback, interval)` - 创建循环定时器,interval单位为秒
2. `SL:ScheduleOnce(callback, delay)` - 创建单次延迟执行,delay单位为秒
3. 定时器回调函数中返回`true`继续执行,返回`false`停止执行
你之前用的 `SL:Schedule` 是用于帧率控制的,比如每帧更新位置等高频操作,不适合用于秒级倒计时。
下次遇到定时器问题记得区分清楚:
- 高频更新(每帧):`SL:Schedule`
- 单次延迟:`SL:ScheduleOnce`
- 循环定时(秒级):`GUI:schedule`
不懂的可以问浮生梦老师!