你的代码已经实现了基本的转盘逻辑,但缺少了转盘转动时的核心光效动画。根据浮生梦老师前端课程内容,主要问题在于`lottery:showEffect()`函数中的动画实现方式不正确。
以下是修正后的关键代码部分:
```lua
-- 显示当前格子灯光特效
function lottery:showEffect()
-- 获取当前格子的灯光控件
local imgEffect = lottery.effect[lottery.currentIndex]
if imgEffect then
GUI:stopAllActions(imgEffect)
GUI:setOpacity(imgEffect, 255) -- 设置显示光效
-- 创建淡入淡出序列动作
local fadeIn = GUI:ActionFadeTo(0.1, 255) -- 0.1秒淡入到完全不透明
local delay = GUI:DelayTime(lottery.time) -- 停留时间
local fadeOut = GUI:ActionFadeTo(0.1, 0) -- 0.1秒淡出到透明
local callback = GUI:CallFunc(function()
lottery:onLightFadeOut()
end)
-- 执行动作序列
GUI:runAction(imgEffect, GUI:ActionSequence(fadeIn, delay, fadeOut, callback))
end
end
-- 设置灯光淡出
function lottery:onLightFadeOut()
-- 旋转计数
lottery.spinCount = lottery.spinCount + 1
-- 判断是否到达目标位置
local isTarget = (lottery.currentIndex == lottery.targetIndex and lottery.loopCount >= lottery.loop)
if isTarget then
lottery.wating = false -- 抽奖结束,解锁状态,允许再次抽奖
GUI:Button_setTitleText(lottery.ui.start_but, "开始转盘")
GUI:Button_setBright(lottery.ui.start_but, true)
-- 获取目标格子的灯光控件
local imagelight = lottery.effect[lottery.currentIndex]
-- 创建闪烁效果
lottery.fadeIn = function()
if imagelight and not GUI:Win_IsNull(imagelight) then
GUI:stopAllActions(imagelight)
local fadeInAction = GUI:ActionFadeTo(0.2, 255)
local callback = GUI:CallFunc(lottery.fadeOut)
GUI:runAction(imagelight, GUI:ActionSequence(fadeInAction, callback))
end
end
lottery.fadeOut = function()
if imagelight and not GUI:Win_IsNull(imagelight) then
GUI:stopAllActions(imagelight)
local fadeOutAction = GUI:ActionFadeTo(0.2, 0)
local callback = GUI:CallFunc(lottery.fadeIn)
GUI:runAction(imagelight, GUI:ActionSequence(fadeOutAction, callback))
end
end
if imagelight and not GUI:Win_IsNull(imagelight) then
lottery.fadeIn() -- 开始闪烁效果
end
-- 显示中奖奖励信息
local reward = lottery.config.items[lottery.targetIndex] -- 获取奖励配置信息
if reward then
SL:ShowSystemMessage("恭喜获得: " .. reward.name)
end
else
-- 还没到达目标位置,继续移动到下一个格子
lottery:nextEffect()
end
end
```
**主要修改点:**
1. **动画序列修复**:使用`GUI:ActionSequence()`将多个动作组合起来,实现"淡入→停留→淡出→回调"的完整流程
2. **正确的淡入淡出函数**:使用`GUI:ActionFadeTo(time, opacity)`而不是`ActionFadeIn/Out`
3. **动作回调**:使用`GUI:CallFunc()`来执行回调函数
4. **空值检查**:添加了`GUI:Win_IsNull()`检查,避免操作已销毁的控件
**参数说明:**
- `GUI:ActionFadeTo(time, opacity)`:在指定时间内渐变到指定透明度
- `GUI:DelayTime(duration)`:延迟指定时间
- `GUI:ActionSequence(...)`:将多个动作按顺序组合
- `GUI:CallFunc(callback)`:执行回调函数
这样修改后,转盘转动时每个格子会有明显的淡入淡出光效,到达目标位置后会有闪烁效果,视觉体验会好很多。