local leishen = {}
-- 主处理函数
function leishen.link(actor, msgid, arg1, arg2, arg3, sMsg)
-- 检查配置
if not cfg_leishen or not next(cfg_leishen) then
lib:msg(actor, "配置错误", 1)
return
end
local rlv_str = getplaydef(ac
你的特效CD没有实现的主要问题在于前端代码中缺少对进度条和倒计时的正确更新机制。让我帮你修复这个问题:
```lua
local leishen = {}
-- 全局变量存储特效相关数据
leishen.effectData = nil
leishen.sceneLayer = nil
leishen.mainLoadingBar = nil
leishen.effectObjects = {}
leishen.cdTimer = nil
local function showEffect(msgID, p1, p2, p3, msgData)
local data = SL:JsonDecode(msgData, false) or {}
SL:Print("特效数据", data)
-- 清理之前的特效
if leishen.sceneLayer and not GUI:Win_IsNull(leishen.sceneLayer) then
GUI:removeFromParent(leishen.sceneLayer)
end
-- 重置数据
leishen.effectData = data
leishen.effectObjects = {}
local num = data.num or 0
local eff1, eff2 = data.preEff, data.startEff
-- 创建父节点作为容器
leishen.sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 主倒计时进度条
local mainImage = GUI:Image_Create(leishen.sceneLayer, "Main_Image", 700, 500, "res/erdalu/leishen/jdt/leishen.png")
if GUI:Win_IsNotNull(mainImage) then
GUI:setVisible(mainImage, true)
end
leishen.mainLoadingBar = GUI:LoadingBar_Create(mainImage, "Main_LoadingBar", 0, 0, "res/erdalu/leishen/jdt/1.png", 0)
GUI:LoadingBar_setPercent(leishen.mainLoadingBar, 100) -- 初始100%
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(leishen.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")
-- 存储对象
leishen.effectObjects[i] = {
circleEffect = circleEffect,
countdownText = countdownText,
posX = posX,
posY = posY,
eff2 = eff2,
count = 40
}
end
end
-- 更新倒计时
local function updateCountdown(timeData)
if not timeData then return end
local totalCount = timeData.totalCount or 0
local percent = timeData.percent or 0
SL:Print("更新倒计时:", "剩余时间:", totalCount, "百分比:", percent)
-- 更新主进度条
if leishen.mainLoadingBar and not GUI:Win_IsNull(leishen.mainLoadingBar) then
GUI:LoadingBar_setPercent(leishen.mainLoadingBar, percent)
end
-- 更新所有特效的倒计时文本
if leishen.effectData then
local num = leishen.effectData.num or 0
for i = 1, num do
if leishen.effectObjects[i] and leishen.effectObjects[i].countdownText then
if not GUI:Win_IsNull(leishen.effectObjects[i].countdownText) then
GUI:Text_setString(leishen.effectObjects[i].countdownText, tostring(totalCount))
end
end
end
-- 倒计时结束,播放技能特效
if totalCount == 0 then
local num = leishen.effectData.num or 0
local eff2 = leishen.effectData.startEff
for i = 1, num do
if leishen.effectObjects[i] then
-- 清理倒计时文本
if not GUI:Win_IsNull(leishen.effectObjects[i].countdownText) then
GUI:removeFromParent(leishen.effectObjects[i].countdownText)
end
-- 清理预警特效
if not GUI:Win_IsNull(leishen.effectObjects[i].circleEffect) then
GUI:removeFromParent(leishen.effectObjects[i].circleEffect)
end
-- 创建技能特效
local skillEffect = GUI:Effect_Create(leishen.sceneLayer, "skill_effect_" .. i,
leishen.effectObjects[i].posX, leishen.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
end
end
-- 延迟清理场景
SL:ScheduleOnce(function()
if leishen.sceneLayer and not GUI:Win_IsNull(leishen.sceneLayer) then
GUI:removeFromParent(leishen.sceneLayer)
leishen.sceneLayer = nil
leishen.effectData = nil
leishen.effectObjects = {}
end
-- 清理定时器
if leishen.cdTimer then
SL:UnRegisterLuaNetMsg(2028)
leishen.cdTimer = nil
end
end, 2.0)
end
end
end
-- 注册网络消息
SL:RegisterLuaNetMsg(2027, showEffect)
-- 注册倒计时更新消息
SL:RegisterLuaNetMsg(2028, function(msgID, p1, p2, p3, timeDataStr)
local timeData = SL:JsonDecode(timeDataStr, false) or {}
updateCountdown(timeData)
end)
function leishen.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
leishen.parent = lib:openwin(param1, nil, true, param4) or {}
leishen.param1 = param1
end
if param2 then
leishen.param2 = param2
end
if param3 then
leishen.param3 = param3
end
if leishen.parent.cfg and next(leishen.parent.cfg) then
leishen.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(leishen.parent._parent)
end
end
end)
end
-- 创建特效数据
function leishen.createButtonEffectData()
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1280
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT") or 720
local num = math.random(3, 5) -- 3-5个特效
return {
num = num,
preEff = 83373, -- 预警特效ID
startEff = 83383, -- 技能特效ID
mapdata = {}
}
end
function leishen.upmain()
if not leishen.parent or not leishen.parent._ui then
return
end
local ui = leishen.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U50"))
local var1 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U51"))
SL:Print("获取当前焚天等级 var =", var)
-- 设置升级按钮
if GUI:Win_IsNotNull(ui["Button_2"]) then
GUI:addOnClickEvent(ui["Button_2"], function()
SL:Print("点击Button_2")
GUI:delayTouchEnabled(ui["Button_2"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 0, 0, jsonstr)
GUI:setVisible(ui.Image_1, true)
GUI:setVisible(ui.Image_4, false)
GUI:setVisible(ui.Image_5, false)
GUI:setVisible(ui.Button_5, false)
GUI:setVisible(ui.Button_1, true)
GUI:Win_CloseAll()
end)
end
if GUI:Win_IsNotNull(ui["Button_3"]) then
GUI:addOnClickEvent(ui["Button_3"], function()
SL:Print("点击Button_3")
GUI:delayTouchEnabled(ui["Button_3"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 1, 0, jsonstr)
GUI:setVisible(ui.Image_1, false)
GUI:setVisible(ui.Image_4, true)
GUI:setVisible(ui.Image_5, false)
GUI:setVisible(ui.Button_5, false)
GUI:setVisible(ui.Button_1, false)
GUI:Win_CloseAll()
end)
end
if GUI:Win_IsNotNull(ui["Button_4"]) then
GUI:addOnClickEvent(ui["Button_4"], function()
local currentU51 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U51"))
if not currentU51 or currentU51 < 2 then
SL:Print("条件不满足,U51 =", currentU51)
return
end
SL:Print("点击Button_4")
GUI:delayTouchEnabled(ui["Button_4"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 2, 0, jsonstr)
GUI:setVisible(ui.Image_1, false)
GUI:setVisible(ui.Image_4, false)
GUI:setVisible(ui.Image_5, true)
GUI:setVisible(ui.Button_5, true)
GUI:setVisible(ui.Button_1, false)
end)
end
if GUI:Win_IsNotNull(ui["Button_1"]) then
GUI:addOnClickEvent(ui["Button_1"], function()
SL:Print("点击Button_1")
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 3, 0, jsonstr)
end)
end
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 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)
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "leishen", function(data)
if data and data.opera == 1 then
leishen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "leishen", function(data)
if next(data) and (data.key == "U50" or data.key == "U51") then
leishen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "leishen", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "leishen" then
-- 清理特效相关数据
if leishen.sceneLayer and not GUI:Win_IsNull(leishen.sceneLayer) then
GUI:removeFromParent(leishen.sceneLayer)
end
-- 取消注册消息
SL:UnRegisterLuaNetMsg(2028)
-- 清理事件注册
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "leishen")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "leishen")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "leishen")
-- 重置数据
leishen.effectData = nil
leishen.sceneLayer = nil
leishen.mainLoadingBar = nil
leishen.effectObjects = {}
leishen.cdTimer = nil
end
end)
return leishen
```
主要问题修复:
1. **全局数据管理**:添加了`leishen.effectData`、`leishen.effectObjects`等全局变量来管理特效状态
2. **进度条初始化**:`GUI:LoadingBar_setPercent(leishen.mainLoadingBar, 100)`确保进度条从100%开始
3. **倒计时更新**:独立的`updateCountdown`函数专门处理倒计时更新逻辑
4. **消息注册**:确保2028消息正确注册,接收后端发送的倒计时数据
5. **清理机制**:在倒计时结束后正确清理所有特效对象和定时器
6. **窗口关闭处理**:在`LUA_EVENT_CLOSEWIN`事件中清理所有相关资源
现在你的特效CD应该能正常工作了,进度条会从100%逐渐减少到0%,同时显示剩余时间。