function refined:refreshWin(p3, msgData)
refined.eqInfo = SL:JsonDecode(msgData)
for k, v in pairs(refined.eqInfo) do
Sl:print(k)
if k <= 11 then
local buttonName = "but_"..v.pos
i
看了你的代码,有几个问题需要修正:
1. **按钮创建问题**:你试图在按钮上创建图片,但`GUI:Image_Create`的第一个参数应该是父节点对象,而不是按钮本身。你需要先获取按钮的父节点。
2. **图片层级问题**:即使创建了图片,也需要设置正确的层级和位置。
3. **按钮状态判断逻辑**:`hasEquip`的判断方式有问题,应该直接使用后端返回的`hasEquip`字段。
这是修正后的代码:
```lua
function refined:refreshWin(p3, msgData)
refined.eqInfo = SL:JsonDecode(msgData)
for k, v in pairs(refined.eqInfo) do
SL:print(k)
if k <= 11 then
local buttonName = "but_"..v.pos
if refined.ui[buttonName] then
GUI:Button_setTitleText(refined.ui[buttonName], v.name or "未装备")
local hasEquip = not string.find(v.name, "未装备")
-- 设置按钮状态
GUI:setTouchEnabled(refined.ui[buttonName], hasEquip) -- 有装备可点击,无装备不可点击
GUI:Button_setGrey(refined.ui[buttonName], not hasEquip) -- 有装备高亮,无装备灰暗
end
end
end
if refined.eqInfo[12] then
local buttonName = "but_"..p3
-- 修正:先检查按钮是否存在
if refined.ui[buttonName] then
-- 获取按钮的父节点
local parent = GUI:getParent(refined.ui[buttonName])
if refined.eqInfo[12].hasEquip == false then
-- 在按钮的父节点上创建高亮图片,并设置层级在按钮之上
local highLight = GUI:Image_Create(parent, "highBright",
GUI:getPosition(refined.ui[buttonName]).x,
GUI:getPosition(refined.ui[buttonName]).y,
"res/public/1900000582.png")
-- 设置图片大小与按钮一致
GUI:setContentSize(highLight,
GUI:getContentSize(refined.ui[buttonName]).width,
GUI:getContentSize(refined.ui[buttonName]).height)
-- 设置图片层级在按钮之上
GUI:setLocalZOrder(highLight, GUI:getLocalZOrder(refined.ui[buttonName]) + 1)
end
end
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
end
end
```
**主要修改点:**
1. **图片创建位置修正**:使用`GUI:getParent()`获取按钮的父节点,然后在父节点上创建图片
2. **坐标对齐**:使用`GUI:getPosition()`获取按钮坐标,让图片与按钮位置一致
3. **大小设置**:使用`GUI:getContentSize()`获取按钮大小,让图片与按钮大小一致
4. **层级设置**:使用`GUI:setLocalZOrder()`确保图片显示在按钮之上
**调试建议:**
在创建图片后添加调试信息:
```lua
SL:print("创建高亮图片,父节点:", parent)
SL:print("按钮位置:", GUI:getPosition(refined.ui[buttonName]).x, GUI:getPosition(refined.ui[buttonName]).y)
SL:print("图片创建成功:", highLight)
```
这样应该能解决按钮文字和图片都创建不出来的问题。如果还有问题,检查一下按钮的父节点是否正确获取。