local heinu = {}
-- 在模块级别存储动画状态,这样关闭窗口也不会丢失
local animatedCoalImages = {}
local animatedTitleImages = {}
function heinu.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
你的代码问题在于动画状态记录导致控件初始就隐藏了。根据浮生梦老师前端课程内容,`animatedCoalImages`和`animatedTitleImages`这两个表在模块级别存储,关闭窗口也不会重置,所以再次打开界面时,这些控件直接被设置为隐藏状态。
**问题分析:**
1. `heinu.refreshAnimationsOnly()`函数中,如果控件已记录在动画状态表中,会直接隐藏控件
2. `heinu.upmain()`函数中也会根据动画状态表隐藏控件
3. 由于动画状态表是模块级变量,关闭窗口后仍然保留,导致再次打开时控件被隐藏
**解决方案:**
需要将动画状态表与窗口生命周期绑定,而不是模块级别。修改如下:
```lua
local heinu = {}
function heinu.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
heinu.parent = lib:openwin(param1, nil, true, param4) or {}
heinu.param1 = param1
-- 每次打开窗口时重置动画状态
heinu.animatedCoalImages = {}
heinu.animatedTitleImages = {}
end
if param2 then
heinu.param2 = param2
end
if param3 then
heinu.param3 = param3
end
if heinu.parent.cfg and next(heinu.parent.cfg) then
heinu.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(heinu.parent._parent)
end
end
end)
end
function heinu.refreshAnimationsOnly()
if not heinu.parent or not heinu.parent._ui then
return
end
local ui = heinu.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U47")) or 0
-- 使用窗口级别的动画状态表
local animatedCoalImages = heinu.animatedCoalImages or {}
local animatedTitleImages = heinu.animatedTitleImages or {}
local function addAnimationIfNeeded(imgTitle, imgCoal, delayTime)
-- 如果已经播放过,直接隐藏控件
if animatedCoalImages[imgCoal] then
if not GUI:Win_IsNull(ui[imgCoal]) then
GUI:setVisible(ui[imgCoal], false)
GUI:setOpacity(ui[imgCoal], 0)
end
return
end
if animatedTitleImages[imgTitle] then
if not GUI:Win_IsNull(ui[imgTitle]) then
GUI:setVisible(ui[imgTitle], false)
GUI:setOpacity(ui[imgTitle], 0)
end
return
end
-- 检查控件是否存在
if GUI:Win_IsNull(ui[imgTitle]) or GUI:Win_IsNull(ui[imgCoal]) then
return
end
-- 标记为已执行
animatedCoalImages[imgCoal] = true
animatedTitleImages[imgTitle] = true
-- 保存回窗口状态
heinu.animatedCoalImages = animatedCoalImages
heinu.animatedTitleImages = animatedTitleImages
-- 初始状态设置
GUI:setVisible(ui[imgTitle], true)
GUI:setOpacity(ui[imgTitle], 0)
GUI:setVisible(ui[imgCoal], true)
GUI:setOpacity(ui[imgCoal], 255)
-- 感谢标题淡入动画
local titleFadeIn = GUI:ActionFadeTo(0.5, 255)
GUI:runAction(ui[imgTitle], titleFadeIn)
-- 延迟执行后续动画
SL:ScheduleOnce(function()
-- 黑煤球向下移动并淡出
local moveDown = GUI:ActionMoveBy(0.5, 0, -100)
local coalFadeOut = GUI:ActionFadeTo(0.5, 0)
local coalSequence = GUI:ActionSequence(
moveDown,
coalFadeOut,
GUI:ActionCallFunc(function()
GUI:setVisible(ui[imgCoal], false)
end)
)
GUI:runAction(ui[imgCoal], coalSequence)
-- 感谢标题淡出
local titleFadeOut = GUI:ActionFadeTo(0.5, 0)
local titleSequence = GUI:ActionSequence(
titleFadeOut,
GUI:ActionCallFunc(function()
GUI:setVisible(ui[imgTitle], false)
end)
)
GUI:runAction(ui[imgTitle], titleSequence)
end, 1.0 + (delayTime or 0))
end
-- 根据等级添加动画
if var >= 1 then
addAnimationIfNeeded("Image_9", "Image_2", 0)
end
if var >= 2 then
addAnimationIfNeeded("Image_10", "Image_3", 0.1)
end
if var >= 3 then
addAnimationIfNeeded("Image_11", "Image_4", 0.2)
end
if var >= 4 then
addAnimationIfNeeded("Image_12", "Image_5", 0.3)
end
if var >= 5 then
addAnimationIfNeeded("Image_13", "Image_6", 0.4)
end
if var >= 6 then
addAnimationIfNeeded("Image_14", "Image_7", 0.5)
end
if var >= 7 then
addAnimationIfNeeded("Image_15", "Image_8", 0.6)
end
end
function heinu.upmain()
if not heinu.parent or not heinu.parent._ui then
return
end
local ui = heinu.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U47"))
SL:Print("获取当前焚天等级 var =", var)
if not GUI:Win_IsNull(ui.Text_1) then
GUI:Text_setString(ui.Text_1, (var or 0))
end
-- 使用窗口级别的动画状态表
local animatedCoalImages = heinu.animatedCoalImages or {}
local animatedTitleImages = heinu.animatedTitleImages or {}
-- 直接隐藏已经播放过动画的控件
for i = 2, 8 do
local imgName = "Image_" .. i
if ui[imgName] and not GUI:Win_IsNull(ui[imgName]) then
if animatedCoalImages[imgName] then
GUI:setVisible(ui[imgName], false)
GUI:setOpacity(ui[imgName], 0)
else
GUI:setVisible(ui[imgName], true)
GUI:setOpacity(ui[imgName], 255)
end
end
end
for i = 9, 15 do
local imgName = "Image_" .. i
if ui[imgName] and not GUI:Win_IsNull(ui[imgName]) then
if animatedTitleImages[imgName] then
GUI:setVisible(ui[imgName], false)
GUI:setOpacity(ui[imgName], 0)
else
GUI:setVisible(ui[imgName], false)
GUI:setOpacity(ui[imgName], 0)
end
end
end
-- 调用只添加动画的刷新函数
heinu.refreshAnimationsOnly()
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
SL:Print("点击了第1个按钮")
GUI:delayTouchEnabled(ui.Button_1, 0.2)
local msg = {}
msg.Script = heinu.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, heinu.param2, 0, 0, jsonstr)
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "heinu", function(data)
if data and data.opera == 1 then
heinu.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "heinu", function(data)
if next(data) and data.key == "U47" then
heinu.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "heinu", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "heinu" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "heinu")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "heinu")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "heinu")
-- 清理窗口级别的动画状态
heinu.animatedCoalImages = nil
heinu.animatedTitleImages = nil
end
end)
return heinu
```
**主要修改:**
1. **移除模块级变量**:将`animatedCoalImages`和`animatedTitleImages`从模块级别移到`heinu`表中
2. **窗口初始化时重置**:在`heinu.main()`函数中每次打开窗口时重新初始化动画状态表
3. **窗口关闭时清理**:在`LUA_EVENT_CLOSEWIN`事件中清理动画状态
4. **统一引用**:所有函数都使用`heinu.animatedCoalImages`和`heinu.animatedTitleImages`来访问动画状态
这样修改后,每次打开窗口都会重新开始动画,关闭窗口后动画状态会被清理,下次打开时控件会正常显示并播放动画。