DengJiTiSheng = Class("DengJiTiSheng", BaseLogic)
function DengJiTiSheng:init()
self._cfg = Util:requireCfg(self.mCfg.clientObjPath)
end
function DengJiTiSheng:main(npcData)
self.npcData = npcData or {}
self.npcID = self.npcD
根据浮生梦老师前端课程内容,你的代码中存在几个问题导致获取等级为0:
1. **函数调用错误**:`SL:Get_LEVEL()` 这个函数不存在,应该使用 `SL:GetMetaValue`
2. **数据传递问题**:`syncData` 方法中获取的 `curLevel` 是51,但传递给 `updateUI` 时没有正确处理
修改后的代码:
```lua
function DengJiTiSheng:updateUI(actor, data)
local ui = self.ui
-- 优先使用传入的data参数(来自后端的curLevel)
local level = 0
if data and type(data) == "number" then
level = data
SL:print("使用后端数据,等级为:", level)
else
-- 如果data无效,尝试从元数据获取
level = SL:GetMetaValue("H.LEVEL") or 0
SL:print("使用元数据,等级为:", level)
end
-- 移除旧的TextAtlas
if ui.TextAtlas_1 and not GUI:Win_IsNull(ui.TextAtlas_1) then
GUI:removeFromParent(ui.TextAtlas_1)
end
-- 创建新的TextAtlas显示等级
local TextAtlas_1 = GUI:TextAtlas_Create(ui.RichText_3, "TextAtlas_1", 151, 2, tostring(level), "Default/TextAtlas.png", 14, 18, ".")
-- 配置查找逻辑
local curnLevel = ""
local curnNum = 0
for _, cfg in ipairs(self._cfg) do
local lowStr, highStr = string.match(cfg.level, "(%d+)%-(%d+)")
if lowStr and highStr then
local lowNum = tonumber(lowStr)
local highNum = tonumber(highStr)
if level >= lowNum and level <= highNum then
curnLevel = cfg.level
curnNum = tonumber(cfg.num)
break
end
end
end
-- 如果没找到配置,使用默认配置
if curnLevel == "" and #self._cfg > 0 then
curnLevel = self._cfg[1].level
curnNum = tonumber(self._cfg[1].num)
end
-- 移除旧的RichText
if ui.RichText_4 and not GUI:Win_IsNull(ui.RichText_4) then
GUI:removeFromParent(ui.RichText_4)
end
-- 创建新的RichText
local str = string.format("<font color='#ff9b00' size='14'>【%s级】</font><font color='#ffffff' size='14'>每升1级需要:</font><font color='#00ff00' size='14'>等级丹*%d个</font>",
curnLevel, curnNum)
local RichText_4 = GUI:RichText_Create(ui.Img_BG, "RichText_4", 80, 110, str, 300, 16, 0xFFFFFFFF)
-- 按钮事件
if ui.Button_1 and not GUI:Win_IsNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
self:request("TiSheng")
end)
end
end
```
**主要修改点:**
1. 优先使用 `data` 参数(后端返回的51级)
2. 添加了空值判断和打印调试信息
3. 使用 `tostring(level)` 确保TextAtlas接收字符串参数
4. 添加了组件空值检查
你的后端数据是正确的(51级),问题在于前端没有正确处理这个数据。记得检查 `syncData` 方法是否正确调用了 `updateUI` 并传递了 `curLevel` 参数。