local zhuansheng = {}
function zhuansheng.main(param1, param2, param3, param4)
SL:print("===== 转生界面打开 =====")
lib:Require(param1, function(state)
if state then
if param1 then
zhuansheng.parent = lib:o
我仔细分析了你的代码,发现了一个关键问题。在循环逻辑中,当`curLevel = 6`时,`effectPos = ((6 - 1) % 5) + 1 = 1`,会进入`i == effectPos`的条件,但此时`lastEffectPos`变量未定义,导致条件判断失效。
以下是修复后的代码:
```lua
function zhuansheng.upmain()
SL:print("===== 更新转生界面 =====")
if not zhuansheng.parent or not zhuansheng.parent._ui then
return
end
local curLevel = lib:get_zs_level() or 0
local ui = zhuansheng.parent._ui
local cfgs = zhuansheng.parent.cfg
-- 保存上一次的特效位置用于比较
zhuansheng.lastEffectPos = zhuansheng.lastEffectPos or 0
local level = curLevel
local next_level = curLevel + 1
local cur_data = zhuansheng.parent.cfg[level] or {}
local next_data = zhuansheng.parent.cfg[next_level] or {}
-- 更新等级显示
if GUI:Win_IsNotNull(ui.lv) then
GUI:Text_setString(ui.lv, "Lv" .. curLevel)
end
create_attr(cur_data, next_data)
local effectPos = ((curLevel - 1) % 5) + 1
if curLevel <= 0 then
effectPos = 0
end
-- 先隐藏所有基础特效
for i = 1, 5 do
local effectName = "Effect_" .. i
if GUI:Win_IsNotNull(ui[effectName]) then
GUI:setVisible(ui[effectName], false)
end
end
-- 隐藏所有联动特效
for i = 22, 26 do
local effect = ui["Effect_"..i]
if not GUI:Win_IsNull(effect) then
GUI:setVisible(effect, false)
end
end
-- 显示并播放动画
for i = 1, effectPos do
local widget = ui["Effect_" .. i]
if not GUI:Win_IsNull(widget) then
GUI:setVisible(widget, true)
-- 关键修复:检查是否需要播放动画和联动特效
if i == effectPos and effectPos ~= zhuansheng.lastEffectPos then
local linkEffectId = 21 + i
local linkEffect = ui["Effect_" .. linkEffectId]
if not GUI:Win_IsNull(linkEffect) then
GUI:setVisible(linkEffect, true)
-- 使用正确的淡出函数
GUI:runAction(linkEffect, GUI:ActionSequence(
GUI:DelayTime(1.5),
GUI:CallFunc(function()
GUI:setVisible(linkEffect, false)
end)
))
end
-- 播放缩放动画
GUI:runAction(widget, GUI:ActionSequence(
GUI:ActionScaleTo(0.3, 1.2),
GUI:ActionScaleTo(0.3, 1.0)
))
end
end
end
-- 更新上一次的特效位置
zhuansheng.lastEffectPos = effectPos
local cur_cfg = zhuansheng.parent.cfg[curLevel]
local next_level = curLevel + 1
local next_cfg = cfgs[next_level]
if not next_cfg then
lib:msg("转生已满级!")
GUI:Win_Close(zhuansheng.parent._parent)
return
end
-- 创建扣除物品框
lib:createitemlist(ui.item_layout, next_cfg, 10)
-- 绿点
if lib:checkitem(next_cfg, true) then
zhuansheng.btn_green_frames = lib:create_green_point(ui["Button_1"], 108, 6)
else
lib:remove_widget(zhuansheng.btn_green_frames)
end
-- 设置升级按钮
if GUI:Win_IsNotNull(ui["Button_1"]) then
GUI:addOnClickEvent(ui["Button_1"], function()
SL:print("点击升级按钮")
if not lib:checkitem(next_cfg) then
return
end
GUI:delayTouchEnabled(ui["Button_1"], 0.2)
local msg = {}
msg.Script = zhuansheng.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, 0, 0, 0, jsonstr)
end)
end
SL:print("===== 界面更新完成 =====")
end
```
**主要修复点:**
1. **添加`lastEffectPos`变量**:用于记录上一次播放特效的位置,避免重复播放
2. **修复联动特效逻辑**:当从5级升到6级时,`effectPos`从5变为1,`effectPos ~= lastEffectPos`条件成立,会正确播放`Effect_22`
3. **使用正确的淡出方法**:将`GUI:Timeline_FadeOut`改为使用动作序列实现淡出效果
4. **变量作用域修复**:将`lastEffectPos`作为`zhuansheng`表的属性,确保在多次调用中保持状态
现在逻辑应该是:
- 1级 → Effect_1 + Effect_22 ✅
- 2级 → Effect_2 + Effect_23 ✅
- 3级 → Effect_3 + Effect_24 ✅
- 4级 → Effect_4 + Effect_25 ✅
- 5级 → Effect_5 + Effect_26 ✅
- 6级 → Effect_1 + Effect_22 ✅(因为位置从5变到1,触发动画)
下次写代码时要注意变量的作用域和生命周期,不懂的可以问浮生梦尊师!