Bag = {}
function Bag.Init(isWin32)
-- 网格配置
Bag._ScrollHeight = isWin32 and 214 or 320 -- 容器滚动区域的高度
Bag._PWidth = isWin32 and 338 or 500 -- 容器可见区域 宽
Bag._PHeight = isWin32 and 214 or 320 -- 容器可见区域 高
Bag._IW
根据你的代码,我看到你想要将左侧背包中的物品移动到右侧容器格子中显示。你的代码已经有了物品数据获取和槽位创建,但缺少将物品实际显示在右侧格子中的关键部分。
主要问题在于 `Bag.PutItemsInSlots()` 函数中只设置了 `slot.hasItem = true`,但没有创建实际的物品显示控件。以下是需要修改的关键部分:
```lua
function Bag.PutItemsInSlots()
if not Bag._itemSlots or not Bag._itemList then
SL:Print("物品数据或槽位未初始化")
return
end
SL:Print("开始放置物品到槽位,找到物品数量:" .. #Bag._itemList)
-- 确保只放置实际找到的物品数量
local displayCount = math.min(#Bag._itemList, 10)
for i = 1, 10 do
local slot = Bag._itemSlots[i]
if slot and slot.bg and not GUI:Win_IsNull(slot.bg) then
-- 先清理已有的物品显示
if slot.icon then
GUI:removeFromParent(slot.icon)
slot.icon = nil
end
if slot.count then
GUI:removeFromParent(slot.count)
slot.count = nil
end
if slot.name then
GUI:removeFromParent(slot.name)
slot.name = nil
end
if i <= displayCount then
-- 在格子中放入物品
local itemData = Bag._itemList[i]
SL:Print("在槽位 " .. i .. " 中放入物品:" .. itemData.name .. " 数量:" .. itemData.count)
slot.hasItem = true
-- 创建物品图标
slot.icon = GUI:Image_Create(slot.bg,
"item_icon_" .. i,
0, 0,
"res/public/1900000612.png") -- 使用默认图标
GUI:setAnchorPoint(slot.icon, 0.5, 0.5)
GUI:setPosition(slot.icon, 43, 43) -- 居中显示
GUI:setContentSize(slot.icon, 80, 80)
-- 创建物品数量显示
slot.count = GUI:Text_Create(slot.bg,
"item_count_" .. i,
60, 10,
12, "#ffffff",
"x" .. itemData.count)
GUI:Text_setTextVerticalAlignment(slot.count, 1)
-- 创建物品名称显示
slot.name = GUI:Text_Create(slot.bg,
"item_name_" .. i,
43, 70,
10, "#ffffff",
itemData.displayName or itemData.name)
GUI:Text_setTextVerticalAlignment(slot.name, 1)
GUI:setAnchorPoint(slot.name, 0.5, 0)
else
-- 清空槽位
slot.hasItem = false
end
end
end
-- 显示放入的物品信息
for i, itemData in ipairs(Bag._itemList) do
if i <= 10 then
SL:Print("槽位 " .. i .. ": " .. itemData.name .. " x" .. itemData.count)
end
end
end
```
另外,你还需要在 `Bag.InitItemContainer()` 函数中确保槽位初始化正确:
```lua
function Bag.InitItemContainer()
-- 获取物品滚动容器
Bag._itemScrollView = Bag._ui["ScrollView_equipment"]
if GUI:Win_IsNull(Bag._itemScrollView) then
SL:Print("物品滚动容器不存在")
return
end
-- 设置滚动方向为垂直
GUI:ScrollView_setDirection(Bag._itemScrollView, 1)
-- 设置容器内边距
GUI:ScrollView_setInnerContainerSize(Bag._itemScrollView, 100, 700)
-- 创建物品格子
Bag._itemSlots = {}
local slotWidth = 86
local slotHeight = 86
local startY = 700 - 95
local spacing = -20
for i = 1, 10 do
local slotBg = GUI:Image_Create(Bag._itemScrollView,
"item_slot_bg_" .. i,
-2,
startY - (i-1) * (slotHeight + spacing),
"res/zhuansheng2.0/4.png")
GUI:setContentSize(slotBg, slotWidth, slotHeight)
Bag._itemSlots[i] = {
bg = slotBg,
hasItem = false,
icon = nil, -- 添加图标引用
count = nil, -- 添加数量引用
name = nil -- 添加名称引用
}
end
-- 获取物品数据
Bag.GetItemData()
end
```
这样修改后,当你在 `Bag.GetItemData()` 中获取到物品数据后,`Bag.PutItemsInSlots()` 函数会自动在右侧容器格子中显示对应的物品图标、数量和名称。
如果你想要更简单的只显示图标,可以去掉名称和数量的显示,只保留图标部分。