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:29:51 |
👁️ 阅读: 8
我来帮你优化这个切换动画,加入缩放效果。根据浮生梦老师前端课程内容,我会使用已有的动作函数来实现:
```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)
GUI:setOpacity(bg, 0)
-- 设置初始缩放为0(完全缩小)
GUI:setScale(container, 0)
GUI:setScale(bg, 0)
-- 创建动画序列
local function createAnimation()
-- 第一步:同时执行淡入、宽度拉伸和缩放动画
local fadeInExpandAndScale = GUI:ActionSpawn(
GUI:ActionFadeIn(0.5), -- 0.5秒淡入
GUI:ActionScaleTo(0.5, 1.0), -- 0.5秒从0缩放到100%
GUI:CallFunc(function()
-- 自定义宽度扩展函数
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)
)
-- 第二步:等待0.5秒显示
local wait = GUI:DelayTime(0.5)
-- 第三步:同时执行淡出、宽度收缩和缩小动画
local fadeOutShrinkAndScale = GUI:ActionSpawn(
GUI:ActionFadeOut(0.5), -- 0.5秒淡出
GUI:ActionScaleTo(0.5, 0), -- 0.5秒从100%缩小到0
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)
)
-- 第四步:隐藏容器
local hideAction = GUI:CallFunc(function()
GUI:setVisible(container, false)
GUI:removeFromParent(container)
end)
-- 组合所有动作
local sequence = GUI:ActionSequence(
fadeInExpandAndScale, -- 同时淡入+拉伸+放大
wait, -- 等待
fadeOutShrinkAndScale, -- 同时淡出+收缩+缩小
hideAction -- 隐藏
)
-- 播放动画
GUI:runAction(container, sequence)
end
-- 开始动画
createAnimation()
end
```
**主要改进:**
1. **缩放效果**:使用`GUI:ActionScaleTo()`实现从0到1的放大和从1到0的缩小
2. **同步动画**:使用`GUI:ActionSpawn()`让淡入/淡出、宽度变化和缩放同时进行
3. **初始状态**:设置初始缩放为0,实现"先缩小尺寸"的效果
4. **资源清理**:动画结束后移除容器,避免内存泄漏
**动画流程:**
1. 0.5秒内:同时完成淡入、宽度拉伸到200、缩放从0到1
2. 等待0.5秒
3. 0.5秒内:同时完成淡出、宽度收缩到0、缩放从1到0
4. 隐藏并移除容器
这样就能实现"先缩小尺寸再放大到原尺寸"的视觉效果了!
我来帮你优化这个切换动画,加入缩放效果。根据浮生梦老师前端课程内容,我会使用已有的动作函数来实现:
```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)
GUI:setOpacity(bg, 0)
-- 设置初始缩放为0(完全缩小)
GUI:setScale(container, 0)
GUI:setScale(bg, 0)
-- 创建动画序列
local function createAnimation()
-- 第一步:同时执行淡入、宽度拉伸和缩放动画
local fadeInExpandAndScale = GUI:ActionSpawn(
GUI:ActionFadeIn(0.5), -- 0.5秒淡入
GUI:ActionScaleTo(0.5, 1.0), -- 0.5秒从0缩放到100%
GUI:CallFunc(function()
-- 自定义宽度扩展函数
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)
)
-- 第二步:等待0.5秒显示
local wait = GUI:DelayTime(0.5)
-- 第三步:同时执行淡出、宽度收缩和缩小动画
local fadeOutShrinkAndScale = GUI:ActionSpawn(
GUI:ActionFadeOut(0.5), -- 0.5秒淡出
GUI:ActionScaleTo(0.5, 0), -- 0.5秒从100%缩小到0
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)
)
-- 第四步:隐藏容器
local hideAction = GUI:CallFunc(function()
GUI:setVisible(container, false)
GUI:removeFromParent(container)
end)
-- 组合所有动作
local sequence = GUI:ActionSequence(
fadeInExpandAndScale, -- 同时淡入+拉伸+放大
wait, -- 等待
fadeOutShrinkAndScale, -- 同时淡出+收缩+缩小
hideAction -- 隐藏
)
-- 播放动画
GUI:runAction(container, sequence)
end
-- 开始动画
createAnimation()
end
```
**主要改进:**
1. **缩放效果**:使用`GUI:ActionScaleTo()`实现从0到1的放大和从1到0的缩小
2. **同步动画**:使用`GUI:ActionSpawn()`让淡入/淡出、宽度变化和缩放同时进行
3. **初始状态**:设置初始缩放为0,实现"先缩小尺寸"的效果
4. **资源清理**:动画结束后移除容器,避免内存泄漏
**动画流程:**
1. 0.5秒内:同时完成淡入、宽度拉伸到200、缩放从0到1
2. 等待0.5秒
3. 0.5秒内:同时完成淡出、宽度收缩到0、缩放从1到0
4. 隐藏并移除容器
这样就能实现"先缩小尺寸再放大到原尺寸"的视觉效果了!
← 返回问答库