错误在第28行:`attempt to index local 'data' (a number value)`
问题分析:
你的`updateUI`函数定义是:
```lua
function YuanShenChuQiao:updateUI(actor,data)
```
但在错误日志中,调用的是`[549,"updateUI",{"index":0}]`,这意味着`data`参数实际上是一个数字(549),而不是你期望的table。
修改方法:
```lua
function YuanShenChuQiao:updateUI(arg1, data)
-- 修正参数处理
local ui = self.ui
local index = 0
-- 判断参数类型
if type(arg1) == "table" and arg1.level then
-- 第一个参数是table的情况
index = arg1.level or 0
elseif type(data) == "table" and data.level then
-- 第二个参数是table的情况
index = data.level or 0
elseif type(data) == "number" then
-- data是数字的情况(从错误日志看)
index = data
end
SL:Print("index:", index)
--显示物品框及数量
local cost = self._cfg[1].cost or ""
UIManager.createCostItem(self.ui, cost, ui.ListView_2, "1", false)
local att = tonumber(self._cfg[1].att) or 0
UILib:removeFromParent(ui.showAtt)
ui.showAtt = GUI:RichTextFCOLOR_Create(ui.ListView_1, "rich3"..self.npcID, 10, 0, UIManager.attToRichText(att, 255), 230, 16)
--进度条显示
UILib:removeFromParent(ui.LoadingBar_1)
local LoadingBar_1 = GUI:LoadingBar_Create(ui.Image_2, "LoadingBar_1", 15, 3, "res/custom/yuanshenchuqiao/3.png", 0)
GUI:LoadingBar_setPercent(LoadingBar_1, index)
UILib:addOnClickEvent(ui.Button_1, function()
self:request("JiHuo", { index = index })
end)
end
```
**关键点**:
1. 引擎消息传递时,`updateUI`可能收到的是数字参数而不是table
2. 使用`type()`函数判断参数类型
3. 处理多种可能的参数格式
**建议**:
- 检查调用`updateUI`的地方,确保参数传递正确
- 使用`SL:Print()`打印参数类型来调试
- 下次写函数时,先验证参数类型,避免这种索引错误
不懂的地方可以问浮生梦老师!