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
-- 创建父节
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-24 14:43:25 |
👁️ 阅读: 4
根据你的代码,要实现点击按钮5后特效持续40秒,需要确保特效容器和所有相关组件在40秒内不被提前移除。以下是修改后的关键部分:
```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)
-- 1. 先关闭其他界面,但保留特效界面
GUI:Win_CloseAll()
-- 2. 创建特效容器(放在关闭之后)
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 3. 创建特效数据
local effectData = leishen.createButtonEffectData()
-- 4. 添加随机位置
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
-- 5. 直接调用特效函数,避免网络消息冲突
showEffect(2027, 0, 0, 0, SL:JsonEncode(effectData))
-- 6. 发送升级请求
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 4, 0, jsonstr)
-- 7. 修正图片路径
local image6 = GUI:Image_Create(sceneLayer, "Image_6", 0, 0, "res/er dalu/leishen/jdt/leishen.png")
GUI:setVisible(image6, true)
-- 8. 创建LoadingBar
local loadingBar = GUI:LoadingBar_Create(image6, "LoadingBar_1", 100, 100, "res/er dalu/leishen/jdt/1.png")
GUI:LoadingBar_setPercent(loadingBar, 100)
-- 9. 倒计时逻辑 - 确保40秒内不删除容器
local count = 40
local timerID = "loadingbar_countdown"
SL:Unschedule(timerID)
SL:Schedule(timerID, function()
count = count - 1
if count >= 0 then
local percent = (count / 40) * 100
GUI:LoadingBar_setPercent(loadingBar, percent)
if count == 0 then
-- 40秒后才移除容器和组件
GUI:setVisible(image6, false)
GUI:LoadingBar_setPercent(loadingBar, 100)
SL:Unschedule(timerID)
GUI:removeFromParent(sceneLayer)
end
else
SL:Unschedule(timerID)
end
end, 1, true)
end)
end
```
主要修改点:
1. 确保`sceneLayer`容器在40秒内不被提前移除
2. 倒计时逻辑只在`count == 0`时才移除容器
3. 保持`showEffect`函数中的特效逻辑不变(它已经包含了40秒的倒计时)
4. 定时器使用`SL:Schedule`进行循环执行
这样修改后,点击按钮5会触发特效并持续显示40秒,40秒后才会移除所有相关组件。
根据你的代码,要实现点击按钮5后特效持续40秒,需要确保特效容器和所有相关组件在40秒内不被提前移除。以下是修改后的关键部分:
```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)
-- 1. 先关闭其他界面,但保留特效界面
GUI:Win_CloseAll()
-- 2. 创建特效容器(放在关闭之后)
local sceneLayer = GUI:Win_Create("EffectLayer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 3. 创建特效数据
local effectData = leishen.createButtonEffectData()
-- 4. 添加随机位置
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
-- 5. 直接调用特效函数,避免网络消息冲突
showEffect(2027, 0, 0, 0, SL:JsonEncode(effectData))
-- 6. 发送升级请求
local msg = {}
msg.Script = leishen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, leishen.param2, 4, 0, jsonstr)
-- 7. 修正图片路径
local image6 = GUI:Image_Create(sceneLayer, "Image_6", 0, 0, "res/er dalu/leishen/jdt/leishen.png")
GUI:setVisible(image6, true)
-- 8. 创建LoadingBar
local loadingBar = GUI:LoadingBar_Create(image6, "LoadingBar_1", 100, 100, "res/er dalu/leishen/jdt/1.png")
GUI:LoadingBar_setPercent(loadingBar, 100)
-- 9. 倒计时逻辑 - 确保40秒内不删除容器
local count = 40
local timerID = "loadingbar_countdown"
SL:Unschedule(timerID)
SL:Schedule(timerID, function()
count = count - 1
if count >= 0 then
local percent = (count / 40) * 100
GUI:LoadingBar_setPercent(loadingBar, percent)
if count == 0 then
-- 40秒后才移除容器和组件
GUI:setVisible(image6, false)
GUI:LoadingBar_setPercent(loadingBar, 100)
SL:Unschedule(timerID)
GUI:removeFromParent(sceneLayer)
end
else
SL:Unschedule(timerID)
end
end, 1, true)
end)
end
```
主要修改点:
1. 确保`sceneLayer`容器在40秒内不被提前移除
2. 倒计时逻辑只在`count == 0`时才移除容器
3. 保持`showEffect`函数中的特效逻辑不变(它已经包含了40秒的倒计时)
4. 定时器使用`SL:Schedule`进行循环执行
这样修改后,点击按钮5会触发特效并持续显示40秒,40秒后才会移除所有相关组件。
← 返回问答库