[LUA-print] LUA ERROR: [string "scripts/game/view/layersui/player_layer/Playe..."]:23: attempt to call field 'UpdateBaseAttri' (a nil value)
[LUA-print] stack traceback:
[string "scripts/main.lua"]:14: in function 'UpdateBaseAttri'
[
你的代码中缺少 `PlayerBaseAtt.UpdateBaseAttri` 函数的定义,导致调用时出现 `nil` 错误。根据浮生梦老师前端课程内容,你需要添加这个函数来更新角色基础属性。
参考你提供的代码,正确的 `UpdateBaseAttri` 函数应该类似以下结构:
```lua
function PlayerBaseAtt.UpdateBaseAttri()
local list = PlayerBaseAtt._ui.ListView_base
GUI:removeAllChildren(list)
-- 遍历基础属性字符串数组
for k, v in ipairs(PlayerBaseAtt._baseAttributeStr) do
local listData = {
str = v,
strValue = getAttValue(k) -- 注意:getAttValue也需要定义
}
PlayerBaseAtt.CreateAttri(list, listData)
end
-- 脚本添加的额外属性处理
local variableInfo = SL:GetMetaValue("EX_ATTR")
if variableInfo then
local variableList = string.split(variableInfo, "|")
for i, v in ipairs(variableList) do
local attList = string.split(v, "#")
if attList[1] and attList[2] then
local listData = {
str = attList[1],
strValue = attList[2]
}
local cell = PlayerBaseAtt.CreateAttri(list, listData)
if string.find(attList[2], "&<(.-)>&") then
SL:CustomAttrWidgetAdd(attList[2], cell.Text_attValue)
end
end
end
end
end
```
你还需要添加以下内容:
1. **定义 `_baseAttributeStr` 数组**:
```lua
PlayerBaseAtt._baseAttributeStr = {
"职 业",
"等 级",
"当前经验",
"升级经验",
"真实充值",
"魂师等级",
"武 魂",
}
```
2. **定义 `getAttValue` 函数**(如果不存在):
```lua
local function getAttValue(key)
local str = ""
if key == 1 then
str = SL:GetMetaValue("JOB_NAME")
elseif key == 2 then
local level = SL:GetMetaValue("LEVEL")
local reinLv = SL:GetMetaValue("RELEVEL")
if reinLv and reinLv > 0 then
str = string.format("%s转%s级", reinLv, level)
else
str = string.format("%s级", level)
end
elseif key == 3 then
str = SL:GetMetaValue("EXP")
elseif key == 4 then
str = SL:GetMetaValue("MAXEXP")
-- 继续添加其他属性的处理...
end
return str
end
```
3. **在 `main` 函数末尾添加**:
```lua
PlayerBaseAtt._index = 0
PlayerBaseAtt.UpdateBaseAttri()
```
**错误原因分析**:你的代码中定义了 `main` 函数,但没有定义 `UpdateBaseAttri` 函数,当其他地方调用 `PlayerBaseAtt.UpdateBaseAttri()` 时就会报错。
建议检查调用 `UpdateBaseAttri` 的地方,确保函数已正确定义。如果还有问题,可以继续询问!