写一个显示血魔球动画特效的脚本
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-23 00:55:53 |
👁️ 阅读: 12
根据浮生梦老师前端课程内容,我来帮你写一个显示血魔球动画特效的脚本:
```lua
-- 创建动画特效函数
function createMagicBallEffect(parentWidget, effectType, positionX, positionY)
-- 参数说明:
-- parentWidget: 父节点控件
-- effectType: 特效类型 (1=HP, 2=MP, 3=FHP)
-- positionX, positionY: 显示位置坐标
if GUI:Win_IsNull(parentWidget) then
SL:print("父节点无效")
return
end
local prefixList = {"hp_", "mp_", "fhp_"}
local tagList = {"HPSFX", "MPSFX", "FHPSFX"}
if effectType < 1 or effectType > 3 then
SL:print("特效类型错误,有效范围1-3")
return
end
local prefix = prefixList[effectType] or "hp_"
local tag = tagList[effectType] or "HPSFX"
-- 创建动画对象
local animation = GUI:Animation_Create()
local frameSize = {width = 0, height = 0}
-- 添加动画帧(假设有10帧图片)
for i = 1, 10 do
local path = string.format("res/private/mhp_ui/%s%s.png", prefix, i)
if SL:IsFileExist(path) then
local sprite = GUI:Sprite_Create(-1, "temp_sprite", 0, 0, path)
frameSize = GUI:getContentSize(sprite)
GUI:Animation_addSpriteFrame(animation, GUI:Sprite_getSpriteFrame(sprite))
end
end
-- 设置动画参数
GUI:Animation_setDelayPerUnit(animation, 0.1) -- 每帧0.1秒
GUI:Animation_setLoops(animation, 1) -- 播放1次
GUI:Animation_setRestoreOriginalFrame(animation, true) -- 恢复原始帧
-- 创建显示精灵
local effectSprite = GUI:Sprite_Create(parentWidget, tag, positionX, positionY)
-- 运行动画
GUI:Sprite_runAction(effectSprite, GUI:Animate(animation))
-- 设置定时器自动移除(5秒后)
SL:ScheduleOnce(
function()
if not GUI:Win_IsNull(effectSprite) then
GUI:stopAllActions(effectSprite)
GUI:removeFromParent(effectSprite)
end
end,
5
)
return effectSprite
end
-- 使用示例
local function showEffectExample()
-- 创建父窗口
local parent = GUI:Win_Create("EffectWindow", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 显示HP血球特效在屏幕中央
local hpEffect = createMagicBallEffect(parent, 1, 200, 200)
-- 显示MP魔球特效
local mpEffect = createMagicBallEffect(parent, 2, 300, 200)
-- 显示FHP血球特效
local fhpEffect = createMagicBallEffect(parent, 3, 400, 200)
end
-- 注册事件触发(如果需要通过事件触发)
SL:RegisterLUAEvent("SHOW_MAGICBALL_EFFECT", "YourModule", function(data)
if data and data.type then
local parent = GUI:getChildByName(GUI:getRootNode(), "EffectWindow")
if GUI:Win_IsNull(parent) then
parent = GUI:Win_Create("EffectWindow", 0, 0, 0, 0, false, false, true, false, false, false, 1)
end
createMagicBallEffect(parent, data.type, data.x or 200, data.y or 200)
end
end)
```
**脚本功能说明:**
1. **createMagicBallEffect函数**:核心函数,负责创建和播放血魔球动画
- 支持三种特效类型:HP血球、MP魔球、FHP血球
- 自动加载序列帧图片
- 5秒后自动移除特效
2. **动画参数配置**:
- 每帧播放时间:0.1秒
- 播放次数:1次
- 图片路径:res/private/mhp_ui/ 目录下
- 图片命名格式:hp_1.png, hp_2.png...(共10帧)
3. **内存管理**:
- 使用`GUI:Win_IsNull()`检查控件有效性
- 使用`SL:ScheduleOnce()`定时清理
- 使用`GUI:removeFromParent()`正确移除节点
**注意事项:**
- 确保图片资源存在:res/private/mhp_ui/hp_1.png ~ hp_10.png
- 可以根据需要调整动画帧率和播放次数
- 特效位置坐标可以根据实际需求调整
这个脚本可以直接复制使用,记得根据实际图片资源调整帧数和路径!
← 返回问答库