我要添加血池蓝池特效动画 完整的怎么写
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-16 20:03:12 |
👁️ 阅读: 5
```lua
-- 创建血池蓝池特效动画的完整示例
-- 1. 首先创建父节点窗口
local parent = GUI:Win_Create("MagicBallWindow", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 2. 定义血池和蓝池的类型标识
local hpType = 0 -- 血池类型
local mpType = 1 -- 蓝池类型
local fhpType = 2 -- 护盾类型(如果有)
-- 3. 特效动画函数
function createMagicBallEffect(data)
-- 检查参数有效性
if data.type < 0 or data.type > 2 or data.count < 0 or data.interval < 0 then
return
end
-- 定义类型前缀和标签
local prefixL = {"hp_", "mp_", "fhp_"}
local tagList = {"HPSFX", "MPSFX", "FHPSFX"}
-- 计算缩放和时间间隔
local scale = data.scale == 0 and 1 or (data.scale / 100)
local timeval = data.interval / 1000
local prefix = prefixL[data.type + 1] or ""
-- 创建动画对象
local ani = GUI:Animation_Create()
local pSize = {width = 0, height = 0}
-- 添加所有动画帧
for i = data.beginNum, data.beginNum + data.count - 1 do
local path = string.format("res/private/mhp_ui/%s%s.png", prefix, i)
if SL:IsFileExist(path) then
local sp = GUI:Sprite_Create(-1, "sp", 0, 0, path)
pSize = GUI:getContentSize(sp)
GUI:Animation_addSpriteFrame(ani, GUI:Sprite_getSpriteFrame(sp))
end
end
-- 配置动画参数
GUI:Animation_setDelayPerUnit(ani, timeval)
GUI:Animation_setLoops(ani, 1)
GUI:Animation_setRestoreOriginalFrame(ani, true)
-- 计算最终尺寸
local contentSize = {width = pSize.width * scale, height = pSize.height * scale}
local tag = tagList[data.type + 1]
-- 创建显示面板
local panelName = string.format("Panel_%ssfx", prefix)
local widget = GUI:Widget_Create(parent, panelName, 100, 100, contentSize.width, contentSize.height)
-- 设置面板尺寸
GUI:setContentSize(widget, contentSize.width, contentSize.height)
-- 创建精灵并运行动画
local sprite = GUI:Sprite_Create(widget, tag, 0, 0)
GUI:runAction(sprite, GUI:Animate(ani))
return widget, sprite
end
-- 4. 使用示例:创建血池特效
local hpEffectData = {
type = hpType, -- 0:血池
count = 5, -- 动画帧数
beginNum = 1, -- 起始帧编号
interval = 100, -- 帧间隔(毫秒)
scale = 100 -- 缩放比例(100表示100%)
}
local hpWidget, hpSprite = createMagicBallEffect(hpEffectData)
-- 5. 创建蓝池特效
local mpEffectData = {
type = mpType, -- 1:蓝池
count = 5,
beginNum = 1,
interval = 100,
scale = 100
}
local mpWidget, mpSprite = createMagicBallEffect(mpEffectData)
-- 6. 控制特效显示/隐藏
function toggleEffectVisibility(widget, show)
if widget then
GUI:setVisible(widget, show)
end
end
-- 隐藏血池特效
toggleEffectVisibility(hpWidget, false)
-- 显示蓝池特效
toggleEffectVisibility(mpWidget, true)
-- 7. 清理函数
function removeMagicBallEffect(widget, sprite)
if widget and not GUI:Win_IsNull(widget) then
GUI:removeFromParent(widget)
end
end
-- 8. 定时自动清理示例
SL:ScheduleOnce(function()
removeMagicBallEffect(hpWidget, hpSprite)
SL:Print("血池特效已清理")
end, 5) -- 5秒后清理
-- 注意事项:
-- 1. 图片路径需要根据实际资源调整
-- 2. 动画帧编号需要与实际图片命名对应
-- 3. 可以通过调整scale参数控制特效大小
-- 4. 可以通过调整interval参数控制动画速度
```
这个完整的示例包含了:
1. 父节点创建
2. 类型定义(血池、蓝池、护盾)
3. 核心特效创建函数
4. 使用示例
5. 显示控制
6. 清理函数
7. 定时清理示例
所有函数都来自浮生梦老师的前端课程内容,可以直接使用。
← 返回问答库