local lianhuo = {}
function lianhuo.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
lianhuo.parent = lib:openwin(param1, nil, true, param4) or {}
根据你的代码分析,问题在于网络消息注册时的`lianhuo.parent`参数。在注册时`lianhuo.parent`可能还未初始化,导致回调函数无法正确执行。另外动画逻辑需要调整播放条件。
以下是完整的修正代码:
```lua
local lianhuo = {}
function lianhuo.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
lianhuo.parent = lib:openwin(param1, nil, true, param4) or {}
lianhuo.param1 = param1
end
if param2 then
lianhuo.param2 = param2
end
if param3 then
lianhuo.param3 = param3
end
if lianhuo.parent.cfg and next(lianhuo.parent.cfg) then
lianhuo.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(lianhuo.parent._parent)
end
end
end)
end
-- 在lianhuo.upmain()函数中添加动画逻辑
function lianhuo.upmain()
if not lianhuo.parent or not lianhuo.parent._ui then
return
end
local ui = lianhuo.parent._ui
local cfg = lianhuo.parent.cfg
if not cfg then
return
end
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U48"))
SL:Print("获取当前焚天等级 var =", var)
if var == "" then var = 0 end
var = tonumber(var) or 0
if not GUI:Win_IsNull(ui.Text_1) then
GUI:Text_setString(ui.Text_1, (var or 0))
end
-- 初始化所有Text控件为不可见
local textControls = {ui.Text_2, ui.Text_3, ui.Text_4, ui.Text_5}
for _, text in ipairs(textControls) do
if not GUI:Win_IsNull(text) then
GUI:setVisible(text, false)
GUI:setOpacity(text, 0) -- 设置透明度为0
end
end
-- 定义动画函数
local function playTextAnimation(textWidget, delayTime)
if GUI:Win_IsNull(textWidget) then
return
end
-- 设置初始状态
GUI:setVisible(textWidget, true)
GUI:setOpacity(textWidget, 0)
-- 获取当前位置
local pos = GUI:getPosition(textWidget)
-- 设置起始位置(从下方)
GUI:setPosition(textWidget, pos.x, pos.y - 50)
-- 创建动画序列
local actionSequence = GUI:ActionSequence(
GUI:ActionFadeIn(0.3), -- 淡入效果
GUI:ActionMoveBy(0.5, 0, 300), -- 向上移动
GUI:DelayTime(delayTime or 3), -- 停留时间
GUI:ActionFadeOut(0.3), -- 淡出效果
GUI:CallFunc(function()
GUI:setVisible(textWidget, false)
GUI:setPosition(textWidget, pos.x, pos.y) -- 恢复原位
end)
)
-- 运行动画
GUI:runAction(textWidget, actionSequence)
end
-- 根据条件播放不同动画
if var <= 0 then
-- 打开界面时var<=0播放Text_2动画
if not GUI:Win_IsNull(ui.Text_2) then
playTextAnimation(ui.Text_2, 1)
end
elseif var >= 10 then
-- var>=10时播放Text_5动画
if not GUI:Win_IsNull(ui.Text_5) then
playTextAnimation(ui.Text_5, 1)
end
end
-- 按钮点击事件
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
SL:Print("点击了第1个按钮")
GUI:delayTouchEnabled(ui.Button_1, 0.2)
-- 先隐藏所有可能正在播放的动画
for _, text in ipairs(textControls) do
if not GUI:Win_IsNull(text) then
GUI:stopAllActions(text)
GUI:setVisible(text, false)
end
end
local msg = {}
msg.Script = lianhuo.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, lianhuo.param2, 0, 0, jsonstr)
end)
end
end
-- 修改网络消息注册,移除第三个参数
SL:RegisterLuaNetMsg(58003, function(msgID, p1, p2, p3, msgData)
SL:Print("收到58003消息:", msgData)
local data = SL:JsonDecode(msgData)
if data and data.result then
-- 确保界面存在
if not lianhuo.parent or not lianhuo.parent._ui then
SL:Print("界面不存在")
return
end
local ui = lianhuo.parent._ui
-- 先停止所有动画
local textControls = {ui.Text_2, ui.Text_3, ui.Text_4, ui.Text_5}
for _, text in ipairs(textControls) do
if not GUI:Win_IsNull(text) then
GUI:stopAllActions(text)
GUI:setVisible(text, false)
end
end
-- 根据结果播放不同动画
if data.result == "success" then
SL:Print("播放成功动画")
-- 上交成功播放Text_3动画
if not GUI:Win_IsNull(ui.Text_3) then
GUI:setVisible(ui.Text_3, true)
GUI:setOpacity(ui.Text_3, 0)
local pos = GUI:getPosition(ui.Text_3)
GUI:setPosition(ui.Text_3, pos.x, pos.y - 50)
local actionSequence = GUI:ActionSequence(
GUI:ActionFadeIn(0.3),
GUI:ActionMoveBy(0.5, 0, 50),
GUI:DelayTime(1),
GUI:ActionFadeOut(0.3),
GUI:CallFunc(function()
GUI:setVisible(ui.Text_3, false)
GUI:setPosition(ui.Text_3, pos.x, pos.y)
end)
)
GUI:runAction(ui.Text_3, actionSequence)
end
elseif data.result == "fail" then
SL:Print("播放失败动画")
-- 上交失败播放Text_4动画
if not GUI:Win_IsNull(ui.Text_4) then
GUI:setVisible(ui.Text_4, true)
GUI:setOpacity(ui.Text_4, 0)
local pos = GUI:getPosition(ui.Text_4)
GUI:setPosition(ui.Text_4, pos.x, pos.y - 50)
local actionSequence = GUI:ActionSequence(
GUI:ActionFadeIn(0.3),
GUI:ActionMoveBy(0.5, 0, 50),
GUI:DelayTime(1),
GUI:ActionFadeOut(0.3),
GUI:CallFunc(function()
GUI:setVisible(ui.Text_4, false)
GUI:setPosition(ui.Text_4, pos.x, pos.y)
end)
)
GUI:runAction(ui.Text_4, actionSequence)
end
end
end
end)
--注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "lianhuo", function(data)
if data and data.opera == 1 then
lianhuo.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "lianhuo", function(data)
if next(data) and data.key == "U48" then
lianhuo.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "lianhuo", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "lianhuo" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "lianhuo")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "lianhuo")
end
end)
return lianhuo
```
主要修改点:
1. **移除了`RegisterLuaNetMsg`的第三个参数**:原来传递的`lianhuo.parent`在注册时可能为nil
2. **添加了调试打印**:在网络回调中添加了打印语句,方便跟踪执行流程
3. **增加了界面存在性检查**:在播放动画前确保界面和UI对象存在
4. **修正了动画播放逻辑**:确保成功时播放Text_3,失败时播放Text_4
这样修改后,当服务端返回`result: "success"`时会播放Text_3动画,返回`result: "fail"`时会播放Text_4动画。