function BagItemText:SetStarLevel(star)
local ui_starLv = self._ui["Text_star_lv"]
if not ui_starLv then
return false
end
if not (star and star > 0) then
GUI:setVisible(ui_starLv, false)
return false
e
从你的代码来看,问题在于 `self._itemData` 可能还没有被正确赋值。在 `SetStarLevel` 函数中,你试图访问 `self._itemData.jipinValues`,但 `self._itemData` 可能是在其他地方(比如 `UpdateItemText` 函数中)才被设置的。
根据浮生梦老师前端课程内容,正确的做法是:
1. 确保在调用 `SetStarLevel` 之前,已经通过 `UpdateItemText` 或其他方法将装备数据 `itemData` 设置到 `self._itemData` 中。
2. 在 `SetStarLevel` 函数中,星级 `star` 应该是作为参数传入的,而不是从 `self._itemData` 中获取。
参考课程中的代码,`SetStarLevel` 函数应该这样写:
```lua
function BagItemText:SetStarLevel(star)
local ui_starLv = self._ui["Text_star_lv"]
if not ui_starLv then
return false
end
-- 首先检查星级参数
if not (star and star > 0) then
GUI:setVisible(ui_starLv, false)
return false
end
-- 然后显示星级控件并设置文本
GUI:setVisible(ui_starLv, true)
GUI:Text_setString(ui_starLv, "+" .. star)
-- 设置样式
local arrays = string.split(__GD_Style, "|")
__GD_Style = IsWinPlayMode and arrays[2] or arrays[1]
local styles = SL:Split(__GD_Style, "#")
local colorId = tonumber(styles[1])
local offX = tonumber(styles[2]) or 0
local offY = tonumber(styles[3]) or 0
if colorId then
GUI:Text_setTextColor(ui_starLv, SL:GetHexColorByStyleId(colorId))
local pos = GUI:getPosition(ui_starLv)
GUI:setPosition(ui_starLv, pos.x + offX, pos.y + offY)
end
end
```
如果你需要在显示星级的同时获取极品属性,应该在上层调用 `SetStarLevel` 之前,确保 `self._itemData` 已经被正确赋值。比如在 `UpdateItemText` 函数中:
```lua
function BagItemText:UpdateItemText(itemData)
if not itemData or next(itemData) == nil then
return false
end
self._itemData = itemData -- 这里设置装备数据
-- 这里可以打印极品属性
local jipinValues = self._itemData and self._itemData.jipinValues
if jipinValues then
SL:Print("装备极品属性:", jipinValues)
end
-- 然后设置星级
if self._showStarlevel then
self:SetStarLevel(itemData.Star) -- 传入星级参数
end
end
```
这样修改后,`SetStarLevel` 函数只负责显示星级,而极品属性的处理在 `UpdateItemText` 中完成,代码结构更清晰,也避免了 `self._itemData` 为 `nil` 的问题。