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
-- -- 检
你的代码中有一个关键问题:在 `showEffect` 函数内部,每次收到消息2027都会重新注册一次消息2028,这可能导致之前的定时器消息处理被覆盖或冲突。而且,后端发送的2028消息可能没有被正确触发或接收。
根据浮生梦老师前端课程内容,我建议修改 `showEffect` 函数中处理2028消息的部分,确保定时器逻辑清晰且只注册一次。同时,将需要每秒打印的数据保存到外部变量,使用 `SL:Schedule` 来创建循环定时器。
以下是修改后的关键部分:
```lua
local leishen = {}
-- 用于存储定时器数据的表
local timerData = {
totalCount = 0,
percent = 0,
isRunning = false
}
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
-- 创建父节点作为容器
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 主倒计时进度条
local mainImage = GUI:Image_Create(sceneLayer, "Main_Image", 700, 500, "res/erdalu/leishen/jdt/leishen.png")
if GUI:Win_IsNotNull(mainImage) then
GUI:setVisible(mainImage, true)
end
local mainLoadingBar = GUI:LoadingBar_Create(mainImage, "Main_LoadingBar", 0, 0, "res/erdalu/leishen/jdt/1.png", 0)
GUI:LoadingBar_setPercent(mainLoadingBar, 0) -- 初始0%
-- 存储所有需要管理的对象
local effectObjects = {}
for i = 1, num do
-- 获取屏幕尺寸
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
-- 在屏幕范围内随机位置显示特效
local posX, posY
if data.mapdata and data.mapdata[i] then
posX = data.mapdata[i].x or math.random(100, screenWidth - 100)
posY = data.mapdata[i].y or math.random(100, screenHeight - 100)
else
posX = math.random(100, screenWidth - 100)
posY = math.random(100, screenHeight - 100)
end
-- 创建预警特效
local circleEffect = GUI:Effect_Create(sceneLayer, "circle_effect_" .. i,
posX, posY, 0, eff1, 0, 0, 0, 1)
-- 每个特效的倒计时文本
local countdownText = GUI:Text_Create(circleEffect, "countdown_" .. i,
0, 30, 20, "#FF0000", "40")
-- 存储对象
effectObjects[i] = {
circleEffect = circleEffect,
countdownText = countdownText,
posX = posX,
posY = posY,
eff2 = eff2,
count = 40
}
end
-- 先取消可能存在的旧定时器
if timerData.isRunning then
SL:UnRegisterLuaNetMsg(2028)
timerData.isRunning = false
end
-- 注册接收后端定时器时间的消息
SL:RegisterLuaNetMsg(2028, function(msgID, p1, p2, p3, timeDataStr)
local timeData = SL:JsonDecode(timeDataStr, false) or {}
timerData.totalCount = timeData.totalCount or 0
timerData.percent = timeData.percent or 0
-- 立即打印一次(确保能打印出来)
SL:Print("收到后端时间数据:", "剩余时间:", timerData.totalCount, "百分比:", timerData.percent)
-- 更新主进度条
if not GUI:Win_IsNull(mainLoadingBar) then
GUI:LoadingBar_setPercent(mainLoadingBar, timerData.percent)
end
-- 更新所有特效的倒计时文本
for i = 1, num do
if effectObjects[i] and effectObjects[i].countdownText then
if not GUI:Win_IsNull(effectObjects[i].countdownText) then
GUI:Text_setString(effectObjects[i].countdownText, tostring(timerData.totalCount))
end
-- 当倒计时为0时触发技能特效
if timerData.totalCount == 0 then
if not GUI:Win_IsNull(effectObjects[i].circleEffect) then
-- 创建技能特效
local skillEffect = GUI:Effect_Create(sceneLayer, "skill_effect_" .. i .. math.random(999999),
effectObjects[i].posX, effectObjects[i].posY, 0, eff2, 0, 0, 0, 1)
-- 技能特效动画
if not GUI:Win_IsNull(skillEffect) then
GUI:setOpacity(skillEffect, 255)
local skillAction = GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 2.0),
GUI:DelayTime(0.3),
GUI:ActionFadeTo(0.5, 0),
GUI:CallFunc(function()
GUI:removeFromParent(skillEffect)
end)
)
GUI:runAction(skillEffect, skillAction)
end
-- 清理单个特效
if not GUI:Win_IsNull(effectObjects[i].countdownText) then
GUI:removeFromParent(effectObjects[i].countdownText)
end
if not GUI:Win_IsNull(effectObjects[i].circleEffect) then
GUI:removeFromParent(effectObjects[i].circleEffect)
end
end
end
end
end
-- 所有倒计时结束,清理场景
if timerData.totalCount == 0 then
-- 延迟清理
SL:ScheduleOnce(function()
if not GUI:Win_IsNull(sceneLayer) then
GUI:removeFromParent(sceneLayer)
end
-- 取消注册消息
SL:UnRegisterLuaNetMsg(2028)
timerData.isRunning = false
SL:Print("特效播放结束,清理完成")
end, 1.0) -- 等待1秒让技能特效播放完
end
end)
timerData.isRunning = true
SL:Print("已注册2028消息监听")
end
-- 注册网络消息(保持不变)
SL:RegisterLuaNetMsg(2027, showEffect)
-- ... 其余代码保持不变 ...
```
主要修改点:
1. 添加了 `timerData` 表来存储定时器状态和数据
2. 在注册2028消息前,先取消可能存在的旧注册
3. 将打印语句放在消息处理函数的最开始,确保能执行到
4. 使用 `SL:ScheduleOnce` 来处理延迟清理,替代了 `SL:Sleep`
5. 添加了状态标记 `isRunning` 来管理定时器状态
如果修改后仍然不打印,请检查:
1. 后端是否正确发送了2028消息
2. 消息格式是否符合 `{totalCount: 数字, percent: 数字}` 的JSON格式
3. 在 `showEffect` 函数开头添加 `SL:Print("收到2027消息")` 确认消息能收到
不懂的可以问浮生梦尊师!