根据浮生梦老师前端课程内容,你的代码已经基本实现了功能,但复选框状态获取和发送逻辑需要调整。主要问题是复选框点击事件在循环中创建,但没有关联到具体的装备位置。
以下是修改后的代码:
```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 = {}
refined.refineData = nil
refined.equipData = {}
refined.lock = {0, 0, 0}
refined.currentEquipPos = 0
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:sendRefineRequest()
if not self.currentEquipPos then
SL:print("未选择装备位置")
return
end
-- 获取当前复选框状态
local lockStates = {}
for i = 1, 3 do
local checkBoxName = "CheckBox_" .. i
if self.ui[checkBoxName] then
local isSelected = GUI:CheckBox_isSelected(self.ui[checkBoxName])
lockStates[i] = isSelected and 1 or 0
else
lockStates[i] = 0
end
end
-- 将复选框状态和装备位置一起发送
local lockJson = SL:JsonEncode(lockStates)
SL:SendLuaNetMsg(2013, 13, 3, self.currentEquipPos, lockJson)
SL:print("发送洗练请求 - 装备位置:" .. self.currentEquipPos .. " 锁定状态:" .. lockJson)
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)
-- 洗练按钮
if refined.ui.refineBtn then
GUI:addOnClickEvent(refined.ui.refineBtn, function()
refined:sendRefineRequest()
end)
end
-- 请求装备数据
SL:SendLuaNetMsg(2013, 13, 0, 0, "装备洗练")
-- 初始化装备位置按钮
refined.currentEquipPos = 0
for pos, name in pairs(EquipPos) do
refined.lockStatus[pos] = {0, 0, 0}
local buttonName = "but_" .. pos
if refined.ui[buttonName] then
GUI:addOnClickEvent(refined.ui[buttonName], function()
refined.currentEquipPos = pos
refined:createHighLight(refined.ui[buttonName], pos)
SL:SendLuaNetMsg(2013, 13, 0, pos, "请求装备数据")
end)
end
end
-- 初始化复选框状态
for i = 1, 3 do
local checkBoxName = "CheckBox_" .. i
if refined.ui[checkBoxName] then
GUI:CheckBox_addOnEvent(refined.ui[checkBoxName], function()
local isSelected = GUI:CheckBox_isSelected(refined.ui[checkBoxName])
refined.lock[i] = isSelected and 1 or 0
SL:print("复选框" .. i .. "状态:" .. (isSelected and "锁定" or "未锁定"))
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 or "", "未装备")
GUI:setTouchEnabled(refined.ui[buttonName], hasEquip)
GUI:Button_setGrey(refined.ui[buttonName], not hasEquip)
end
end
end
if refined.eqInfo[12] then
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
end
end
function refined:createHighLight(button, p3)
refined:clearHighLight()
local highBright = GUI:Image_Create(button, "highBright_" .. p3, 0, 0, "res/public/1900000582.png")
GUI:setContentSize(highBright, 105, 32)
GUI:setLocalZOrder(highBright, 1)
refined.currentHighLight = highBright
return highBright
end
function refined:clearHighLight()
if refined.currentHighLight and not GUI:Win_IsNull(refined.currentHighLight) then
GUI:removeFromParent(refined.currentHighLight)
refined.currentHighLight = nil
end
end
SL:RegisterLuaNetMsg(1013, networkCB)
```
主要修改点:
1. **添加了`sendRefineRequest`函数**:专门处理洗练按钮点击,获取当前装备位置和复选框状态
2. **使用消息ID 2013, 参数1=13, 参数2=3**:通过参数2=3表示洗练操作,参数3传递装备位置,msgData传递锁定状态的JSON
3. **优化复选框事件处理**:使用`GUI:CheckBox_addOnEvent`监听复选框状态变化,实时更新锁定状态
4. **简化逻辑**:移除了之前循环中的复选框事件创建,统一在初始化时设置
这样点击洗练按钮时,会同时发送装备位置和三个复选框的锁定状态到后端。