function qiehuan(actor)
local k = SL:GetMetaValue("SCREEN_WIDTH")
local g = SL:GetMetaValue("SCREEN_HEIGHT")
-- 创建父节点
local parent = GUI:Win_Create("MapChangeContainer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
--
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-15 13:14:16 |
👁️ 阅读: 22
我来帮你创建一个带有淡入淡出和宽度变化的动画效果。根据浮生梦老师前端课程内容,我们可以这样实现:
```lua
function qiehuan(actor)
local k = SL:GetMetaValue("SCREEN_WIDTH")
local g = SL:GetMetaValue("SCREEN_HEIGHT")
-- 创建父节点
local parent = GUI:Win_Create("MapChangeContainer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 容器最终宽200,高63
local containerWidth = 200
local containerHeight = 63
-- 计算容器位置(正上方居中)
local posX = k / 2 - containerWidth / 3
local posY = g - 163
-- 创建容器,初始宽度为0
local container = GUI:Layout_Create(parent, "top_container", posX, posY, 0, containerHeight, false)
-- 给容器添加背景图片
local bg = GUI:Image_Create(container, "bg_image", 0, 0, "res/xixi/ditu/3.png")
GUI:setContentSize(bg, 0, containerHeight) -- 初始宽度为0
-- 设置初始透明度为0(完全透明)
GUI:setOpacity(container, 0)
-- 创建动画序列
local function createAnimation()
-- 第一步:0.5秒内淡入并拉伸到目标宽度
local fadeInAndExpand = GUI:ActionSpawn(
GUI:ActionFadeIn(0.5), -- 淡入动作
GUI:ActionSequence(
GUI:CallFunc(function()
-- 使用ActionMoveBy来改变宽度(通过改变x位置和宽度来实现)
-- 注意:这里需要自定义一个宽度变化函数
local function expandWidth()
local currentWidth = 0
local targetWidth = containerWidth
local duration = 0.5
local startTime = os.clock()
local function updateWidth()
local elapsed = os.clock() - startTime
local progress = math.min(elapsed / duration, 1)
local newWidth = currentWidth + (targetWidth - currentWidth) * progress
-- 重新设置容器位置和大小
GUI:setPosition(container, posX - newWidth/2 + containerWidth/2, posY)
GUI:setContentSize(container, newWidth, containerHeight)
GUI:setContentSize(bg, newWidth, containerHeight)
if progress < 1 then
SL:ScheduleOnce(updateWidth, 0.016) -- 约60帧
end
end
SL:ScheduleOnce(updateWidth, 0.016)
end
expandWidth()
end),
GUI:DelayTime(0.5)
)
)
-- 第二步:等待0.5秒
local wait = GUI:DelayTime(0.5)
-- 第三步:0.5秒内淡出并收缩到0宽度
local fadeOutAndShrink = GUI:ActionSpawn(
GUI:ActionFadeOut(0.5), -- 淡出动作
GUI:ActionSequence(
GUI:CallFunc(function()
-- 自定义宽度收缩函数
local function shrinkWidth()
local currentWidth = containerWidth
local targetWidth = 0
local duration = 0.5
local startTime = os.clock()
local function updateWidth()
local elapsed = os.clock() - startTime
local progress = math.min(elapsed / duration, 1)
local newWidth = currentWidth - (currentWidth - targetWidth) * progress
-- 重新设置容器位置和大小
GUI:setPosition(container, posX - newWidth/2 + containerWidth/2, posY)
GUI:setContentSize(container, newWidth, containerHeight)
GUI:setContentSize(bg, newWidth, containerHeight)
if progress < 1 then
SL:ScheduleOnce(updateWidth, 0.016)
end
end
SL:ScheduleOnce(updateWidth, 0.016)
end
shrinkWidth()
end),
GUI:DelayTime(0.5)
)
)
-- 第四步:隐藏容器
local hideAction = GUI:CallFunc(function()
GUI:setVisible(container, false)
end)
-- 组合所有动作
local sequence = GUI:ActionSequence(fadeInAndExpand, wait, fadeOutAndShrink, hideAction)
-- 播放动画
GUI:runAction(container, sequence)
end
-- 开始动画
createAnimation()
end
```
**代码说明:**
1. **初始状态**:容器宽度为0,透明度为0(完全透明)
2. **动画序列**:
- **阶段1(0.5秒)**:同时执行淡入和宽度扩张
- **阶段2(0.5秒)**:保持状态等待
- **阶段3(0.5秒)**:同时执行淡出和宽度收缩
- **阶段4**:隐藏容器
3. **关键函数**:
- `GUI:ActionFadeIn()`:淡入效果
- `GUI:ActionFadeOut()`:淡出效果
- `GUI:ActionSpawn()`:多个动作同时执行
- `GUI:ActionSequence()`:动作顺序执行
- `GUI:DelayTime()`:延迟等待
- `GUI:CallFunc()`:执行自定义函数
4. **自定义宽度动画**:
- 由于996引擎没有直接的宽度变化动作,我使用定时器逐步改变宽度
- 通过`SL:ScheduleOnce()`实现逐帧更新
- 同时调整容器位置保持居中效果
**注意**:宽度变化部分使用了自定义函数,因为996引擎的GUI库中没有直接的宽度变化动作函数。如果你需要更精确的控制,可以考虑使用`GUI:ActionMoveBy()`配合位置和缩放来模拟宽度变化。
我来帮你创建一个带有淡入淡出和宽度变化的动画效果。根据浮生梦老师前端课程内容,我们可以这样实现:
```lua
function qiehuan(actor)
local k = SL:GetMetaValue("SCREEN_WIDTH")
local g = SL:GetMetaValue("SCREEN_HEIGHT")
-- 创建父节点
local parent = GUI:Win_Create("MapChangeContainer", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 容器最终宽200,高63
local containerWidth = 200
local containerHeight = 63
-- 计算容器位置(正上方居中)
local posX = k / 2 - containerWidth / 3
local posY = g - 163
-- 创建容器,初始宽度为0
local container = GUI:Layout_Create(parent, "top_container", posX, posY, 0, containerHeight, false)
-- 给容器添加背景图片
local bg = GUI:Image_Create(container, "bg_image", 0, 0, "res/xixi/ditu/3.png")
GUI:setContentSize(bg, 0, containerHeight) -- 初始宽度为0
-- 设置初始透明度为0(完全透明)
GUI:setOpacity(container, 0)
-- 创建动画序列
local function createAnimation()
-- 第一步:0.5秒内淡入并拉伸到目标宽度
local fadeInAndExpand = GUI:ActionSpawn(
GUI:ActionFadeIn(0.5), -- 淡入动作
GUI:ActionSequence(
GUI:CallFunc(function()
-- 使用ActionMoveBy来改变宽度(通过改变x位置和宽度来实现)
-- 注意:这里需要自定义一个宽度变化函数
local function expandWidth()
local currentWidth = 0
local targetWidth = containerWidth
local duration = 0.5
local startTime = os.clock()
local function updateWidth()
local elapsed = os.clock() - startTime
local progress = math.min(elapsed / duration, 1)
local newWidth = currentWidth + (targetWidth - currentWidth) * progress
-- 重新设置容器位置和大小
GUI:setPosition(container, posX - newWidth/2 + containerWidth/2, posY)
GUI:setContentSize(container, newWidth, containerHeight)
GUI:setContentSize(bg, newWidth, containerHeight)
if progress < 1 then
SL:ScheduleOnce(updateWidth, 0.016) -- 约60帧
end
end
SL:ScheduleOnce(updateWidth, 0.016)
end
expandWidth()
end),
GUI:DelayTime(0.5)
)
)
-- 第二步:等待0.5秒
local wait = GUI:DelayTime(0.5)
-- 第三步:0.5秒内淡出并收缩到0宽度
local fadeOutAndShrink = GUI:ActionSpawn(
GUI:ActionFadeOut(0.5), -- 淡出动作
GUI:ActionSequence(
GUI:CallFunc(function()
-- 自定义宽度收缩函数
local function shrinkWidth()
local currentWidth = containerWidth
local targetWidth = 0
local duration = 0.5
local startTime = os.clock()
local function updateWidth()
local elapsed = os.clock() - startTime
local progress = math.min(elapsed / duration, 1)
local newWidth = currentWidth - (currentWidth - targetWidth) * progress
-- 重新设置容器位置和大小
GUI:setPosition(container, posX - newWidth/2 + containerWidth/2, posY)
GUI:setContentSize(container, newWidth, containerHeight)
GUI:setContentSize(bg, newWidth, containerHeight)
if progress < 1 then
SL:ScheduleOnce(updateWidth, 0.016)
end
end
SL:ScheduleOnce(updateWidth, 0.016)
end
shrinkWidth()
end),
GUI:DelayTime(0.5)
)
)
-- 第四步:隐藏容器
local hideAction = GUI:CallFunc(function()
GUI:setVisible(container, false)
end)
-- 组合所有动作
local sequence = GUI:ActionSequence(fadeInAndExpand, wait, fadeOutAndShrink, hideAction)
-- 播放动画
GUI:runAction(container, sequence)
end
-- 开始动画
createAnimation()
end
```
**代码说明:**
1. **初始状态**:容器宽度为0,透明度为0(完全透明)
2. **动画序列**:
- **阶段1(0.5秒)**:同时执行淡入和宽度扩张
- **阶段2(0.5秒)**:保持状态等待
- **阶段3(0.5秒)**:同时执行淡出和宽度收缩
- **阶段4**:隐藏容器
3. **关键函数**:
- `GUI:ActionFadeIn()`:淡入效果
- `GUI:ActionFadeOut()`:淡出效果
- `GUI:ActionSpawn()`:多个动作同时执行
- `GUI:ActionSequence()`:动作顺序执行
- `GUI:DelayTime()`:延迟等待
- `GUI:CallFunc()`:执行自定义函数
4. **自定义宽度动画**:
- 由于996引擎没有直接的宽度变化动作,我使用定时器逐步改变宽度
- 通过`SL:ScheduleOnce()`实现逐帧更新
- 同时调整容器位置保持居中效果
**注意**:宽度变化部分使用了自定义函数,因为996引擎的GUI库中没有直接的宽度变化动作函数。如果你需要更精确的控制,可以考虑使用`GUI:ActionMoveBy()`配合位置和缩放来模拟宽度变化。
← 返回问答库