你的代码中复选框状态管理有问题,所有按钮共享了同一个`refined.lock`数组。根据浮生梦老师前端课程内容,需要为每个装备位置独立保存复选框状态。
```lua
-- 修改refined表结构,为每个装备位置独立保存锁状态
refined.lockStatus = {} -- 改为table存储每个位置的锁状态
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
refined.lockStatus[x] = {0, 0, 0} -- 每个位置独立的锁状态数组
end
for x, y in pairs(EquipPos) do
local buttonName = "but_"..x
GUI:addOnClickEvent(refined.ui[buttonName], function()
refined.currentEquipPos = x
refined:createHighLight(refined.ui[buttonName], x)
SL:SendLuaNetMsg(2013, 13, 0, x, "装备洗练") -- 放入装备
end)
-- 为每个位置的复选框单独设置点击事件
for i = 1, 3 do
local lockStateName = "CheckBox_"..i
local checkBox = refined.ui[lockStateName]
if checkBox then
-- 移除旧的点击事件(如果有)
-- 重新绑定点击事件,捕获当前位置x
GUI:addOnClickEvent(checkBox, function()
local lockState_i = GUI:CheckBox_isSelected(checkBox)
SL:print("位置"..x.."的复选框"..i.."状态: "..tostring(lockState_i))
-- 更新当前装备位置的锁状态
if lockState_i == true then
refined.lockStatus[x][i] = 1
else
refined.lockStatus[x][i] = 0
end
-- 发送当前装备位置的锁状态到后端
local lockJson = SL:JsonEncode(refined.lockStatus[x])
SL:SendLuaNetMsg(2013, 13, 2, x, lockJson)
end)
end
end
end
-- 首次进入时,在第一个按钮上创建高亮
if refined.ui["but_0"] then
refined:createHighLight(refined.ui["but_0"], 0)
end
end
-- 在refreshWin函数中,根据当前装备位置恢复复选框状态
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
local highBright_Img = "highBright"..p3
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
-- 恢复当前装备位置的复选框状态
if refined.lockStatus[p3] then
for i = 1, 3 do
local checkBoxName = "CheckBox_"..i
local checkBox = refined.ui[checkBoxName]
if checkBox then
-- 根据保存的状态设置复选框
local state = refined.lockStatus[p3][i] == 1
GUI:CheckBox_setSelected(checkBox, state)
end
end
end
end
end
```
**主要修改:**
1. **独立状态存储**:`refined.lockStatus`改为table,每个装备位置有自己的锁状态数组
2. **初始化状态**:在`openWin`中为每个位置初始化锁状态
3. **事件绑定**:复选框点击事件中捕获当前位置`x`,更新对应位置的锁状态
4. **状态恢复**:在`refreshWin`中根据当前装备位置恢复复选框状态
这样每个装备位置的复选框状态就独立了,切换装备时会显示对应位置的锁状态。