for k, v in ipairs(gemList[i]) do
local setData = {}
local selectGem = 0
local itemShow = npc.ui["ItemShow1_"..k]
local itemName = SL:GetMetaValue("ITEM_NAME", v)
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-28 22:35:52 |
👁️ 阅读: 0
你的代码存在两个关键问题:
1. **变量作用域问题**:`selectGem` 在循环内部定义,每次循环都会重置为0,无法记录上一次选中的状态
2. **空值判断问题**:当 `selectGem` 为0时,`npc.ui["ItemShow1_"..selectGem]` 会获取到nil对象
修复后的代码:
```lua
-- 在循环外部定义选中状态变量
local selectGem = 0
for k, v in ipairs(gemList[i]) do
local setData = {}
local itemShow = npc.ui["ItemShow1_"..k]
local itemName = SL:GetMetaValue("ITEM_NAME", v) -- 宝石名
local itemCount = SL:GetMetaValue("ITEM_COUNT", v) -- 宝石数量
setData.index = v
setData.count = itemCount
setData.bgVisible = true
setData.look = true
GUI:ItemShow_updateItem(itemShow, setData)
-- 数量为0时变灰色
if setData.count == 0 then
GUI:ItemShow_setIconGrey(itemShow, true)
else
GUI:ItemShow_setIconGrey(itemShow, false)
-- 添加点击事件
GUI:ItemShow_addReplaceClickEvent(itemShow, function()
-- 如果点击的是当前选中的宝石
if selectGem == k then
-- 取消选中状态
GUI:ItemShow_setItemShowChooseState(itemShow, false)
selectGem = 0
else
-- 取消上一个选中的宝石(如果有的话)
if selectGem ~= 0 then
local lastItemShow = npc.ui["ItemShow1_"..selectGem]
if lastItemShow and not GUI:Win_IsNull(lastItemShow) then
GUI:ItemShow_setItemShowChooseState(lastItemShow, false)
end
end
-- 设置当前宝石为选中状态
GUI:ItemShow_setItemShowChooseState(itemShow, true)
selectGem = k
end
end)
end
end
```
**关键修改点:**
1. **将 `selectGem` 移到循环外部**:确保所有宝石共享同一个选中状态变量
2. **添加空值判断**:使用 `GUI:Win_IsNull()` 检查控件是否有效
3. **逻辑优化**:先检查是否点击已选中的宝石,避免不必要的操作
**学习建议:**
- 注意Lua变量的作用域,循环内定义的变量在每次迭代都会重新初始化
- 在使用GUI控件前,养成检查是否为nil或无效的习惯
- 复杂的交互逻辑建议先画流程图,再写代码
不懂的地方可以随时问浮生梦尊师!
你的代码存在两个关键问题:
1. **变量作用域问题**:`selectGem` 在循环内部定义,每次循环都会重置为0,无法记录上一次选中的状态
2. **空值判断问题**:当 `selectGem` 为0时,`npc.ui["ItemShow1_"..selectGem]` 会获取到nil对象
修复后的代码:
```lua
-- 在循环外部定义选中状态变量
local selectGem = 0
for k, v in ipairs(gemList[i]) do
local setData = {}
local itemShow = npc.ui["ItemShow1_"..k]
local itemName = SL:GetMetaValue("ITEM_NAME", v) -- 宝石名
local itemCount = SL:GetMetaValue("ITEM_COUNT", v) -- 宝石数量
setData.index = v
setData.count = itemCount
setData.bgVisible = true
setData.look = true
GUI:ItemShow_updateItem(itemShow, setData)
-- 数量为0时变灰色
if setData.count == 0 then
GUI:ItemShow_setIconGrey(itemShow, true)
else
GUI:ItemShow_setIconGrey(itemShow, false)
-- 添加点击事件
GUI:ItemShow_addReplaceClickEvent(itemShow, function()
-- 如果点击的是当前选中的宝石
if selectGem == k then
-- 取消选中状态
GUI:ItemShow_setItemShowChooseState(itemShow, false)
selectGem = 0
else
-- 取消上一个选中的宝石(如果有的话)
if selectGem ~= 0 then
local lastItemShow = npc.ui["ItemShow1_"..selectGem]
if lastItemShow and not GUI:Win_IsNull(lastItemShow) then
GUI:ItemShow_setItemShowChooseState(lastItemShow, false)
end
end
-- 设置当前宝石为选中状态
GUI:ItemShow_setItemShowChooseState(itemShow, true)
selectGem = k
end
end)
end
end
```
**关键修改点:**
1. **将 `selectGem` 移到循环外部**:确保所有宝石共享同一个选中状态变量
2. **添加空值判断**:使用 `GUI:Win_IsNull()` 检查控件是否有效
3. **逻辑优化**:先检查是否点击已选中的宝石,避免不必要的操作
**学习建议:**
- 注意Lua变量的作用域,循环内定义的变量在每次迭代都会重新初始化
- 在使用GUI控件前,养成检查是否为nil或无效的习惯
- 复杂的交互逻辑建议先画流程图,再写代码
不懂的地方可以随时问浮生梦尊师!
← 返回问答库