根据浮生梦老师前端课程内容,我来帮你完善洗练界面的高亮显示功能。主要问题在于高亮图片的创建和清除逻辑需要优化。
```lua
SL:print("打开洗练界面")
local refined = {}
EquipPos = {[0] = "衣服",[1] = "武器",[2] = "勋章",[3] = "头盔",[4] = "项链",[5] = "手镯(右)" ,
[6] = "手镯(左)",[7] = "戒指(右)",[8] = "戒指(左)",[10] = "腰带",[11] = "靴子"}
refined.currentAttributes = {
{type = 0, name = "无属性", value = 0, color = "#ffffff"},
{type = 0, name = "无属性", value = 0, color = "#ffffff"},
{type = 0, name = "无属性", value = 0, color = "#ffffff"}
}
refined.lockStatus = {false, false, false}
refined.refineData = nil
-- 存储当前高亮图片的引用
refined.currentHighLight = nil
local function networkCB(msgID, p1 ,p2 ,p3 ,msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
SL:print("参数3: " .. tostring(p3))
SL:print("字符串: " .. tostring(msgData))
SL:print("=======================")
if msgID == 1013 and p1 == 13 then
if p2 == 0 then
refined:openWin()
end
if p2 == 1 then
refined:refreshWin(p3, msgData)
end
end
end
-- 清除当前高亮图片
function refined:clearHighLight()
if self.currentHighLight and not GUI:Win_IsNull(self.currentHighLight) then
GUI:removeFromParent(self.currentHighLight)
self.currentHighLight = nil
end
end
-- 创建高亮图片
function refined:createHighLight(parentButton, pos)
self:clearHighLight()
local highBright = GUI:Image_Create(parentButton, "highBright_" .. pos, 0, 0, "res/public/1900000582.png")
GUI:setContentSize(highBright, 105, 32)
GUI:setLocalZOrder(highBright, 1)
self.currentHighLight = highBright
return highBright
end
function refined:openWin()
-- 创造外部界面
local win_refined = GUI:Win_Create("win_refined", 0, 0, 0, 0)
GUI:LoadExport(win_refined, "refined")
refined.ui = GUI:ui_delegate(win_refined)
GUI:addOnClickEvent(refined.ui.esc, function()
GUI:Win_Close(win_refined)
end)
SL:SendLuaNetMsg(2013, 13, 0, 0, "装备洗练")
refined.currentEquipPos = 0 -- 默认选中衣服位置0
-- 为所有装备按钮绑定点击事件
for x, y in pairs(EquipPos) do
local buttonName = "but_"..x
if refined.ui[buttonName] then
GUI:addOnClickEvent(refined.ui[buttonName], function()
refined.currentEquipPos = x
refined:createHighLight(refined.ui[buttonName], x)
SL:SendLuaNetMsg(2013, 13, 0, x, "装备洗练") -- 放入装备
end)
end
end
-- 首次进入时,在第一个按钮上创建高亮
if refined.ui["but_0"] then
refined:createHighLight(refined.ui["but_0"], 0)
end
end
function refined:refreshWin(p3, msgData)
refined.eqInfo = SL:JsonDecode(msgData)
for k, v in pairs(refined.eqInfo) do
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.eqInfo[12].hasEquip == false then
if refined.ui[buttonName] then
-- 当有装备时,在对应按钮上显示高亮
refined:createHighLight(refined.ui[buttonName], p3)
end
else
SL:print("@$^%#!$@#")
end
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
end
end
SL:RegisterLuaNetMsg(1013, networkCB)
```
**主要改进点:**
1. **添加了高亮管理函数**:
- `refined:clearHighLight()` - 清除当前高亮图片
- `refined:createHighLight(parentButton, pos)` - 创建新的高亮图片
2. **自动清除旧高亮**:每次创建新高亮前都会清除旧的高亮图片
3. **首次进入默认高亮**:在`openWin()`函数末尾添加了首次进入时在第一个按钮上创建高亮
4. **优化点击事件**:在按钮点击事件中直接调用高亮创建函数
5. **使用`GUI:removeFromParent()`**:正确移除高亮图片组件
这样每次点击不同装备按钮时,都会自动清除上一个高亮,并在当前按钮上显示新的高亮效果。首次进入界面时也会在第一个按钮上显示默认高亮。