for i = 1, #gemItems , 5 do
GUI:QuickCell_Create(ui.ListView_1, "QuickCell_Create"..i, 0, 0, 44, 50, function(quickParent)
local item = GUI:Layout_Create(quickParent, "cell_", 0, 0, 55, 55)
local oldobj = nil
🤖 回答引擎: 浮生梦专用 |
🕒 时间: 2026-03-01 21:40:42 |
👁️ 阅读: 9
这段代码存在几个关键问题:
1. **循环步长错误**:`for i = 1, #gemItems , 5 do`中的步长5会导致每次循环跳过4个元素,但内部的`gemItems[i*j]`索引计算方式不正确。
2. **索引计算错误**:`gemItems[i*j]`会导致索引迅速超出范围。例如当i=1时,j=5得到索引5;当i=6时,j=5得到索引30,这不符合预期的每行5个物品的布局。
3. **对象重复创建**:`obj = oldobj`这行代码会导致后续的物品都创建在同一个父节点上,而不是创建新的独立物品。
4. **布局问题**:`x=55`的偏移量只会在j>1时应用,但每次循环都重置了x,没有实现水平排列。
正确的做法应该是:
```lua
for i = 1, #gemItems, 5 do
GUI:QuickCell_Create(ui.ListView_1, "QuickCell_Create"..i, 0, 0, 44, 50, function(quickParent)
local item = GUI:Layout_Create(quickParent, "cell_", 0, 0, 55*5, 55) -- 宽度调整为容纳5个物品
for j = 0, 4 do -- 每行5个
local index = i + j
if index <= #gemItems then
local setData = {
index = gemItems[index] or -1,
look = true,
bgVisible = true,
count = 1,
color = 225
}
local obj = GUI:ItemShow_Create(item, "item"..index, 55*j, 0, setData)
GUI:onClick(obj, function()
SL:print("选中物品:", setData.index, SL:idx2itemname(setData.index))
if self.sfx then
GUI:removeFromParent(self.sfx)
end
self.sfx = GUI:Effect_Create(obj, "sfxddd", 15, 16, 0, 4525, 0, 0, 3, 1)
self.generate = setData.index
end)
end
end
return item
end)
end
```
主要修改:
1. 循环步长保持为5,表示每行处理5个物品
2. 使用`i + j`计算正确的物品索引
3. 每个物品都创建在item布局的不同位置(55*j的偏移)
4. 为每个物品单独创建点击事件
← 返回问答库