[LUA-print] LUA ERROR: [string "scripts/util/util.lua"]:261: Attempt to call a non-callable object.
[LUA-print] stack traceback:
[string "scripts/main.lua"]:14: in function <[string "scripts/main.lua"]:12>
[C]: in function 'listener'
根据错误信息,问题出现在第261行的 `listener` 函数调用。从你的代码结构来看,最可能的原因是 `SL:RegisterLuaNetMsg(2028, ...)` 的回调函数中使用了错误的变量名。
在你的代码中,`showEffect` 函数内部定义了 `effectObjects` 变量,但在2028消息的回调函数中,你试图访问这个局部变量。然而这个回调函数是一个新的闭包,无法直接访问 `showEffect` 函数内部的局部变量。
**错误分析:**
```lua
-- 在showEffect函数中定义的局部变量
local effectObjects = {}
-- 在2028消息回调中尝试访问(这里会报错)
for i = 1, num do -- 这里的num也是局部变量,同样无法访问
if effectObjects[i] and effectObjects[i].countdownText then
-- ...
end
end
```
**解决方案:**
将需要共享的数据存储在全局或模块级变量中:
```lua
local leishen = {}
local effectObjects = {} -- 改为模块级变量
local currentEffectNum = 0 -- 存储当前特效数量
local function showEffect(msgID, p1, p2, p3, msgData)
local data = SL:JsonDecode(msgData, false) or {}
SL:Print("特效数据", data)
local num = data.num or 0
local eff1, eff2 = data.preEff, data.startEff
-- 清空之前的特效数据
effectObjects = {}
currentEffectNum = num
-- ... 其他代码保持不变 ...
for i = 1, num do
-- ... 创建特效的代码 ...
-- 存储到模块级变量
effectObjects[i] = {
circleEffect = circleEffect,
countdownText = countdownText,
posX = posX,
posY = posY,
eff2 = eff2,
remain = 40
}
end
-- 修改2028消息回调
SL:RegisterLuaNetMsg(2028, function(msgID, p1, p2, p3, timeDataStr)
local timeData = SL:JsonDecode(timeDataStr, false) or {}
local totalCount = timeData.totalCount or 0
local percent = timeData.percent or 0
SL:Print("收到后端倒计时:剩余", totalCount, "秒,百分比", percent)
-- 更新主进度条
if not GUI:Win_IsNull(mainLoadingBar) then
GUI:LoadingBar_setPercent(mainLoadingBar, percent)
end
-- 使用currentEffectNum而不是局部变量num
for i = 1, currentEffectNum do
if effectObjects[i] and effectObjects[i].countdownText then
if not GUI:Win_IsNull(effectObjects[i].countdownText) then
GUI:Text_setString(effectObjects[i].countdownText, tostring(totalCount))
effectObjects[i].remain = totalCount
end
-- ... 其他处理代码 ...
end
end
-- ... 清理代码 ...
end)
end
```
**关键修改点:**
1. 将 `effectObjects` 和 `currentEffectNum` 移到函数外部作为模块级变量
2. 在每次创建新特效前清空旧的 `effectObjects`
3. 在2028消息回调中使用 `currentEffectNum` 而不是局部变量 `num`
这样修改后,2028消息的回调函数就能正确访问特效数据了。记得在特效结束后也要清理这些全局数据,避免内存泄漏。