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
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-18 16:11:40 |
👁️ 阅读: 4
```lua
-- 修改 Bag.GetItemData() 函数,使用 SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX") 获取物品数据
function Bag.GetItemData()
Bag._itemList = {}
-- 首先获取背包总格子数
local maxBag = SL:GetMetaValue("MAX_BAG") or 0
-- 遍历目标物品ID列表
for _, itemId in ipairs(Bag._targetItemIds) do
-- 遍历背包所有位置
for pos = 1, maxBag do
-- 使用 ITEM_DATA_BY_MAKEINDEX 获取物品数据
local itemData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)
if itemData and itemData ~= "" and itemData ~= "undefined" then
-- 检查是否为指定物品
if itemData.id == itemId and (itemData.count or 0) > 0 then
local itemName = SL:GetMetaValue("ITEM_NAME", itemId)
table.insert(Bag._itemList, {
id = itemId,
pos = pos,
name = itemName or "未知物品",
count = itemData.count or 0,
icon = SL:GetMetaValue("ITEM_ICON", itemId),
makeIndex = itemData.makeIndex -- 保存makeIndex用于后续操作
})
SL:Print("在背包位置" .. pos .. "找到物品:" .. itemName .. ",数量:" .. (itemData.count or 0))
break -- 找到该物品后跳出内层循环
end
end
end
end
SL:Print("获取到指定物品数量:" .. #Bag._itemList)
-- 如果没有找到任何物品,打印调试信息
if #Bag._itemList == 0 then
SL:Print("背包中未找到指定物品,请确认背包中是否有以下物品:")
for _, itemId in ipairs(Bag._targetItemIds) do
local itemName = SL:GetMetaValue("ITEM_NAME", itemId)
SL:Print("物品ID " .. itemId .. " - " .. (itemName or "未知"))
end
end
-- 更新显示
if Bag.UpdateItemDisplay then
Bag.UpdateItemDisplay()
end
end
-- 修改 Bag.OnItemSlotClick 函数,使用 makeIndex 进行精确操作
function Bag.OnItemSlotClick(slotIndex)
SL:Print("点击物品格子:" .. slotIndex)
local itemData = Bag._itemList[slotIndex]
if itemData then
SL:ShowSystemTips("选中物品:" .. itemData.name .. " x" .. itemData.count)
-- 使用 makeIndex 获取更详细的物品信息
if itemData.makeIndex then
local detailedData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", itemData.makeIndex)
if detailedData then
SL:Print("物品详细信息:")
SL:Print("ID: " .. (detailedData.id or "未知"))
SL:Print("数量: " .. (detailedData.count or 0))
SL:Print("位置: " .. (detailedData.pos or 0))
end
end
else
SL:ShowSystemTips("该槽位为空")
end
end
-- 修改 Bag.InitItemContainer 函数,确保正确获取物品数据
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 - 100
local spacing = -20
for i = 1, 10 do
local slotBg = GUI:Image_Create(Bag._itemScrollView,
"item_slot_bg_" .. i,
10,
startY - (i-1) * (slotHeight + spacing),
"res/zhuansheng2.0/4.png")
GUI:setContentSize(slotBg, slotWidth, slotHeight)
local itemName = GUI:Text_Create(slotBg,
"item_name_" .. i,
slotWidth/2, -15,
12, "#ffffff", "空")
GUI:setAnchorPoint(itemName, 0.5, 0.5)
local itemIcon = GUI:Image_Create(slotBg,
"item_icon_" .. i,
slotWidth/2, slotHeight/2,
"")
GUI:setAnchorPoint(itemIcon, 0.5, 0.5)
GUI:setContentSize(itemIcon, slotWidth - 30, slotHeight - 30)
GUI:setVisible(itemIcon, false)
local itemCount = GUI:Text_Create(slotBg,
"item_count_" .. i,
slotWidth - 10, 10,
10, "#ffffff", "")
GUI:setAnchorPoint(itemCount, 1, 0.5)
GUI:setVisible(itemCount, false)
GUI:addOnClickEvent(slotBg, function()
Bag.OnItemSlotClick(i)
end)
Bag._itemSlots[i] = {
bg = slotBg,
icon = itemIcon,
name = itemName,
count = itemCount
}
end
-- 获取物品数据
Bag.GetItemData()
end
-- 修改 Bag.main 函数中的测试代码
function Bag.main(page)
local parent = GUI:Attach_Parent()
local isWin32 = SL:GetMetaValue("WINPLAYMODE")
GUI:LoadExport(parent, isWin32 and "bag/bag_panel_win32" or "bag/bag_panel")
-- 测试代码
local testItemId = 12256 -- 九天玄气ツ阴阳逆乱
local testItemName = SL:GetMetaValue("ITEM_NAME", testItemId)
if testItemName then
SL:Print("物品ID " .. testItemId .. " 存在,名称:" .. testItemName)
else
SL:Print("警告:物品ID " .. testItemId .. " 不存在或获取失败")
end
-- 测试使用 ITEM_DATA_BY_MAKEINDEX 获取物品数据
local maxBag = SL:GetMetaValue("MAX_BAG") or 0
for pos = 1, maxBag do
local itemData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)
if itemData and itemData.id then
SL:Print("背包位置" .. pos .. ":ID=" .. itemData.id .. ", 数量=" .. (itemData.count or 0))
end
end
Bag._ui = GUI:ui_delegate(parent)
Bag._UI_ScrollView = Bag._ui["ScrollView_items"]
-- 初始化数据
Bag.Init(isWin32)
-- 新增:初始化装备滚动容器
Bag.InitItemContainer()
-- 适配
GUI:setPositionY(Bag._ui["Panel_1"], isWin32 and SL:GetMetaValue("PC_POS_Y") or SL:GetMetaValue("SCREEN_HEIGHT") / 2)
-- 界面拖动
GUI:Win_SetDrag(parent, Bag._ui["Image_bg"])
-- 界面浮起
GUI:Win_SetZPanel(parent, Bag._ui["Image_bg"])
GUI:addOnClickEvent(Bag._ui["Button_close"], function()
SL:CloseBagUI()
end)
-- ... 其他代码保持不变 ...
end
```
主要修改点:
1. **使用 `SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)`** 替代原来的字符串解析方式,直接获取结构化的物品数据
2. **修改 `Bag.GetItemData()` 函数**:
- 直接使用 `itemData.id` 和 `itemData.count` 访问物品信息
- 保存 `makeIndex` 用于后续精确操作
3. **修改 `Bag.OnItemSlotClick()` 函数**:
- 使用 `makeIndex` 获取更详细的物品信息
- 显示更完整的物品数据
4. **更新测试代码**:
- 使用新的API获取物品数据
- 更清晰地显示物品信息
这样修改后,当背包中有指定的5种物品时,它们都会被自动放入容器的格子中显示。
```lua
-- 修改 Bag.GetItemData() 函数,使用 SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX") 获取物品数据
function Bag.GetItemData()
Bag._itemList = {}
-- 首先获取背包总格子数
local maxBag = SL:GetMetaValue("MAX_BAG") or 0
-- 遍历目标物品ID列表
for _, itemId in ipairs(Bag._targetItemIds) do
-- 遍历背包所有位置
for pos = 1, maxBag do
-- 使用 ITEM_DATA_BY_MAKEINDEX 获取物品数据
local itemData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)
if itemData and itemData ~= "" and itemData ~= "undefined" then
-- 检查是否为指定物品
if itemData.id == itemId and (itemData.count or 0) > 0 then
local itemName = SL:GetMetaValue("ITEM_NAME", itemId)
table.insert(Bag._itemList, {
id = itemId,
pos = pos,
name = itemName or "未知物品",
count = itemData.count or 0,
icon = SL:GetMetaValue("ITEM_ICON", itemId),
makeIndex = itemData.makeIndex -- 保存makeIndex用于后续操作
})
SL:Print("在背包位置" .. pos .. "找到物品:" .. itemName .. ",数量:" .. (itemData.count or 0))
break -- 找到该物品后跳出内层循环
end
end
end
end
SL:Print("获取到指定物品数量:" .. #Bag._itemList)
-- 如果没有找到任何物品,打印调试信息
if #Bag._itemList == 0 then
SL:Print("背包中未找到指定物品,请确认背包中是否有以下物品:")
for _, itemId in ipairs(Bag._targetItemIds) do
local itemName = SL:GetMetaValue("ITEM_NAME", itemId)
SL:Print("物品ID " .. itemId .. " - " .. (itemName or "未知"))
end
end
-- 更新显示
if Bag.UpdateItemDisplay then
Bag.UpdateItemDisplay()
end
end
-- 修改 Bag.OnItemSlotClick 函数,使用 makeIndex 进行精确操作
function Bag.OnItemSlotClick(slotIndex)
SL:Print("点击物品格子:" .. slotIndex)
local itemData = Bag._itemList[slotIndex]
if itemData then
SL:ShowSystemTips("选中物品:" .. itemData.name .. " x" .. itemData.count)
-- 使用 makeIndex 获取更详细的物品信息
if itemData.makeIndex then
local detailedData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", itemData.makeIndex)
if detailedData then
SL:Print("物品详细信息:")
SL:Print("ID: " .. (detailedData.id or "未知"))
SL:Print("数量: " .. (detailedData.count or 0))
SL:Print("位置: " .. (detailedData.pos or 0))
end
end
else
SL:ShowSystemTips("该槽位为空")
end
end
-- 修改 Bag.InitItemContainer 函数,确保正确获取物品数据
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 - 100
local spacing = -20
for i = 1, 10 do
local slotBg = GUI:Image_Create(Bag._itemScrollView,
"item_slot_bg_" .. i,
10,
startY - (i-1) * (slotHeight + spacing),
"res/zhuansheng2.0/4.png")
GUI:setContentSize(slotBg, slotWidth, slotHeight)
local itemName = GUI:Text_Create(slotBg,
"item_name_" .. i,
slotWidth/2, -15,
12, "#ffffff", "空")
GUI:setAnchorPoint(itemName, 0.5, 0.5)
local itemIcon = GUI:Image_Create(slotBg,
"item_icon_" .. i,
slotWidth/2, slotHeight/2,
"")
GUI:setAnchorPoint(itemIcon, 0.5, 0.5)
GUI:setContentSize(itemIcon, slotWidth - 30, slotHeight - 30)
GUI:setVisible(itemIcon, false)
local itemCount = GUI:Text_Create(slotBg,
"item_count_" .. i,
slotWidth - 10, 10,
10, "#ffffff", "")
GUI:setAnchorPoint(itemCount, 1, 0.5)
GUI:setVisible(itemCount, false)
GUI:addOnClickEvent(slotBg, function()
Bag.OnItemSlotClick(i)
end)
Bag._itemSlots[i] = {
bg = slotBg,
icon = itemIcon,
name = itemName,
count = itemCount
}
end
-- 获取物品数据
Bag.GetItemData()
end
-- 修改 Bag.main 函数中的测试代码
function Bag.main(page)
local parent = GUI:Attach_Parent()
local isWin32 = SL:GetMetaValue("WINPLAYMODE")
GUI:LoadExport(parent, isWin32 and "bag/bag_panel_win32" or "bag/bag_panel")
-- 测试代码
local testItemId = 12256 -- 九天玄气ツ阴阳逆乱
local testItemName = SL:GetMetaValue("ITEM_NAME", testItemId)
if testItemName then
SL:Print("物品ID " .. testItemId .. " 存在,名称:" .. testItemName)
else
SL:Print("警告:物品ID " .. testItemId .. " 不存在或获取失败")
end
-- 测试使用 ITEM_DATA_BY_MAKEINDEX 获取物品数据
local maxBag = SL:GetMetaValue("MAX_BAG") or 0
for pos = 1, maxBag do
local itemData = SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)
if itemData and itemData.id then
SL:Print("背包位置" .. pos .. ":ID=" .. itemData.id .. ", 数量=" .. (itemData.count or 0))
end
end
Bag._ui = GUI:ui_delegate(parent)
Bag._UI_ScrollView = Bag._ui["ScrollView_items"]
-- 初始化数据
Bag.Init(isWin32)
-- 新增:初始化装备滚动容器
Bag.InitItemContainer()
-- 适配
GUI:setPositionY(Bag._ui["Panel_1"], isWin32 and SL:GetMetaValue("PC_POS_Y") or SL:GetMetaValue("SCREEN_HEIGHT") / 2)
-- 界面拖动
GUI:Win_SetDrag(parent, Bag._ui["Image_bg"])
-- 界面浮起
GUI:Win_SetZPanel(parent, Bag._ui["Image_bg"])
GUI:addOnClickEvent(Bag._ui["Button_close"], function()
SL:CloseBagUI()
end)
-- ... 其他代码保持不变 ...
end
```
主要修改点:
1. **使用 `SL:GetMetaValue("ITEM_DATA_BY_MAKEINDEX", pos)`** 替代原来的字符串解析方式,直接获取结构化的物品数据
2. **修改 `Bag.GetItemData()` 函数**:
- 直接使用 `itemData.id` 和 `itemData.count` 访问物品信息
- 保存 `makeIndex` 用于后续精确操作
3. **修改 `Bag.OnItemSlotClick()` 函数**:
- 使用 `makeIndex` 获取更详细的物品信息
- 显示更完整的物品数据
4. **更新测试代码**:
- 使用新的API获取物品数据
- 更清晰地显示物品信息
这样修改后,当背包中有指定的5种物品时,它们都会被自动放入容器的格子中显示。
← 返回问答库