function fentian.upmain()
if not fentian.parent or not fentian.parent._ui then
return
end
local ui = fentian.parent._ui
local cfg = fentian.parent.cfg
if not cfg then
return
end
local var = tonum
要正确获取对应表的配置,你需要理解配置表的结构和索引方式。从你的代码和配置来看,有几个关键问题需要修正:
## 主要问题分析:
1. **索引计算错误**:你的代码中 `next_level = var + 9` 这个计算方式不对
2. **配置表结构理解**:配置表的键是等级(9, 18, 27...),不是从1开始的连续索引
## 正确的获取方法:
```lua
function fentian.upmain()
if not fentian.parent or not fentian.parent._ui then
return
end
local ui = fentian.parent._ui
local cfg = fentian.parent.cfg
if not cfg then
return
end
local var = tonumber(lib:getstr("U13") or 0)
SL:Print("获取当前焚天等级 var =", var)
-- 检查是否已满级
-- 需要找到配置表中的最大等级
local max_level = 0
for level, _ in pairs(cfg) do
if level > max_level then
max_level = level
end
end
if var >= max_level then
lib:msg("焚天已满级!")
if not GUI:Win_IsNull(ui["Button_1"]) then
GUI:setTouchEnabled(ui["Button_1"], false)
end
return
end
-- 找到下一个等级配置
local next_level = nil
for level, _ in pairs(cfg) do
if level > var then
if not next_level or level < next_level then
next_level = level
end
end
end
if not next_level then
return
end
local next_cfg = cfg[next_level]
if not next_cfg then
return
end
-- 显示下一级属性
if not GUI:Win_IsNull(ui.Text_3) then
GUI:Text_setString(ui.Text_3, "生命值+" .. (next_cfg.cur_attr_8 or 0))
end
if not GUI:Win_IsNull(ui.Text_4) then
GUI:Text_setString(ui.Text_4, "攻击力+" .. (next_cfg.cur_attr_4 or 0))
end
if not GUI:Win_IsNull(ui.Text_5) then
GUI:Text_setString(ui.Text_5, "攻魔道+" .. (next_cfg.cur_attr_9 or 0))
end
if not GUI:Win_IsNull(ui.Text_6) then
GUI:Text_setString(ui.Text_6, "生命加成+" .. (next_cfg.cur_attr_3 or 0))
end
if not GUI:Win_IsNull(ui.Text_7) then
GUI:Text_setString(ui.Text_7, "防御加成+" .. (next_cfg.cur_attr_2 or 0))
end
if not GUI:Win_IsNull(ui.Text_8) then
GUI:Text_setString(ui.Text_8, "打怪伤害+" .. (next_cfg.cur_attr_1 or 0))
end
if not GUI:Win_IsNull(ui.Text_9) then
GUI:Text_setString(ui.Text_9, "打怪伤害+" .. (next_cfg.cur_attr_10 or 0))
end
if not GUI:Win_IsNull(ui.Text_10) then
GUI:Text_setString(ui.Text_10, "防御值+" .. (next_cfg.cur_attr_6 or 0))
end
end
```
## 关键修正点:
1. **使用 `GUI:Win_IsNull()` 而不是 `GUI:Win_IsNotNull()`**
- 根据浮生梦老师前端课程内容,正确的判断是 `GUI:Win_IsNull(widget)`
- 当组件不存在时返回 `true`,所以要用 `not GUI:Win_IsNull()`
2. **正确的配置表遍历逻辑**:
- 先找到配置表中的最大等级
- 然后找到比当前等级大的最小等级作为下一级
3. **配置表索引说明**:
- 你的配置表键是:9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99
- 这些代表的是焚天等级,不是从1开始的连续索引
## 额外建议:
如果你需要按照顺序获取配置,可以这样处理:
```lua
-- 获取有序的等级列表
local function getSortedLevels(cfg)
local levels = {}
for level, _ in pairs(cfg) do
table.insert(levels, level)
end
table.sort(levels)
return levels
end
-- 然后在 upmain 函数中使用:
local sorted_levels = getSortedLevels(cfg)
for i, level in ipairs(sorted_levels) do
if level > var then
next_level = level
break
end
end
```
这样就能正确获取到对应表的配置了。记住,配置表的键就是等级值,直接使用这些值作为索引即可。