function lib:left_move_to_fadeout(widget_name, img_path, move_time, delay_time, fadeout_time, call_fuc)
delay_time = delay_time or 2
fadeout_time = fadeout_time or 1
move_time = move_time or 0.2
local parent = GUI:Attach_UITop()
G
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-23 14:37:36 |
👁️ 阅读: 4
我来逐行解释这个函数:
```lua
-- 函数定义:从左到右移动并淡出
-- 参数说明:
-- widget_name: 控件名称
-- img_path: 图片路径
-- move_time: 移动时间
-- delay_time: 延迟时间
-- fadeout_time: 淡出时间
-- call_fuc: 回调函数
function lib:left_move_to_fadeout(widget_name, img_path, move_time, delay_time, fadeout_time, call_fuc)
-- 设置默认参数
delay_time = delay_time or 2 -- 延迟时间默认2秒
fadeout_time = fadeout_time or 1 -- 淡出时间默认1秒
move_time = move_time or 0.2 -- 移动时间默认0.2秒
-- 获取顶层父节点
local parent = GUI:Attach_UITop()
-- 移除同名控件(避免重复创建)
GUI:removeChildByName(parent, widget_name)
-- 创建图片控件
local img = GUI:Image_Create(parent, widget_name, 0, 0, img_path)
-- 获取图片尺寸
local img_size = GUI:getContentSize(img)
-- 设置锚点为图片中心
GUI:setAnchorPoint(img, 0.5, 0.5)
-- 设置初始位置:图片左半部分在屏幕外
GUI:setPosition(img, -img_size.width / 2, screen_height / 2)
-- 执行动作序列
GUI:runAction(img,
GUI:ActionSequence(
-- 第一步:向右移动
GUI:ActionMoveBy(move_time, img_size.width / 2 + screen_width / 2, 0),
-- 第二步:延迟停留
GUI:DelayTime(delay_time),
-- 第三步:淡出效果
GUI:ActionFadeOut(fadeout_time),
-- 第四步:执行回调并移除
GUI:CallFunc(function()
-- 移除控件
GUI:removeChildByName(parent, widget_name)
-- 执行回调函数(如果存在)
if call_fuc then
call_fuc()
end
end)
)
)
end
```
**函数功能总结:**
这个函数实现了一个"从屏幕左侧进入 → 移动到屏幕中央 → 停留 → 淡出消失"的动画效果。常用于游戏中的提示、公告或特效展示。
**注意点:**
1. 需要提前定义 `screen_height` 和 `screen_width` 变量
2. `GUI:ActionFadeOut` 是996引擎的淡出动作函数
3. 每次调用会先移除同名控件,确保不会重复创建
4. 动作完成后会自动清理资源
**典型使用场景:**
```lua
-- 显示一个公告图片
lib:left_move_to_fadeout("notice_img", "res/public/1900000610.png", 0.3, 3, 0.5, function()
SL:print("公告显示完毕!")
end)
```
这个函数设计得很实用,但要注意浮生梦老师提醒的:创建动作序列时确保每个动作参数正确,特别是时间参数不要传错顺序!
我来逐行解释这个函数:
```lua
-- 函数定义:从左到右移动并淡出
-- 参数说明:
-- widget_name: 控件名称
-- img_path: 图片路径
-- move_time: 移动时间
-- delay_time: 延迟时间
-- fadeout_time: 淡出时间
-- call_fuc: 回调函数
function lib:left_move_to_fadeout(widget_name, img_path, move_time, delay_time, fadeout_time, call_fuc)
-- 设置默认参数
delay_time = delay_time or 2 -- 延迟时间默认2秒
fadeout_time = fadeout_time or 1 -- 淡出时间默认1秒
move_time = move_time or 0.2 -- 移动时间默认0.2秒
-- 获取顶层父节点
local parent = GUI:Attach_UITop()
-- 移除同名控件(避免重复创建)
GUI:removeChildByName(parent, widget_name)
-- 创建图片控件
local img = GUI:Image_Create(parent, widget_name, 0, 0, img_path)
-- 获取图片尺寸
local img_size = GUI:getContentSize(img)
-- 设置锚点为图片中心
GUI:setAnchorPoint(img, 0.5, 0.5)
-- 设置初始位置:图片左半部分在屏幕外
GUI:setPosition(img, -img_size.width / 2, screen_height / 2)
-- 执行动作序列
GUI:runAction(img,
GUI:ActionSequence(
-- 第一步:向右移动
GUI:ActionMoveBy(move_time, img_size.width / 2 + screen_width / 2, 0),
-- 第二步:延迟停留
GUI:DelayTime(delay_time),
-- 第三步:淡出效果
GUI:ActionFadeOut(fadeout_time),
-- 第四步:执行回调并移除
GUI:CallFunc(function()
-- 移除控件
GUI:removeChildByName(parent, widget_name)
-- 执行回调函数(如果存在)
if call_fuc then
call_fuc()
end
end)
)
)
end
```
**函数功能总结:**
这个函数实现了一个"从屏幕左侧进入 → 移动到屏幕中央 → 停留 → 淡出消失"的动画效果。常用于游戏中的提示、公告或特效展示。
**注意点:**
1. 需要提前定义 `screen_height` 和 `screen_width` 变量
2. `GUI:ActionFadeOut` 是996引擎的淡出动作函数
3. 每次调用会先移除同名控件,确保不会重复创建
4. 动作完成后会自动清理资源
**典型使用场景:**
```lua
-- 显示一个公告图片
lib:left_move_to_fadeout("notice_img", "res/public/1900000610.png", 0.3, 3, 0.5, function()
SL:print("公告显示完毕!")
end)
```
这个函数设计得很实用,但要注意浮生梦老师提醒的:创建动作序列时确保每个动作参数正确,特别是时间参数不要传错顺序!
← 返回问答库