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
你的代码有几个问题需要修正:
1. **`GUI:Image_Create`函数参数错误**:创建Image控件时,坐标参数位置不对
2. **装备槽位显示逻辑**:需要修改`Bag.UpdateEquipmentDisplay`函数,不显示空槽位
3. **装备数据获取**:`SL:GetMetaValue("EQUIP_DATA", equipId)`的参数应该是装备类型而不是ID
以下是修正后的代码:
```lua
-- 修改:更新装备显示函数 - 不显示空槽位
function Bag.UpdateEquipmentDisplay()
if not Bag._equipmentSlots then
return
end
for i = 1, 10 do
local slot = Bag._equipmentSlots[i]
if slot and slot.bg and not GUI:Win_IsNull(slot.bg) then
local equipmentData = Bag._equipmentList[i]
if equipmentData then
-- 有装备的情况:显示
GUI:setVisible(slot.bg, true)
if slot.name and not GUI:Win_IsNull(slot.name) then
GUI:Text_setString(slot.name, equipmentData.name)
GUI:Text_setTextColor(slot.name, "#ffffff")
end
if slot.icon and not GUI:Win_IsNull(slot.icon) then
GUI:setVisible(slot.icon, true)
end
else
-- 空槽位:隐藏整个格子
GUI:setVisible(slot.bg, false)
end
end
end
end
-- 修改:初始化装备容器函数 - 修正Image_Create参数
function Bag.InitEquipmentContainer()
-- 获取装备滚动容器
Bag._equipmentScrollView = Bag._ui["ScrollView_equipment"]
if GUI:Win_IsNull(Bag._equipmentScrollView) then
SL:Print("装备滚动容器不存在")
return
end
-- 设置滚动方向为垂直
GUI:ScrollView_setDirection(Bag._equipmentScrollView, 1)
-- 设置容器内边距
GUI:ScrollView_setInnerContainerSize(Bag._equipmentScrollView, 80, 700)
-- 创建装备格子
Bag._equipmentSlots = {}
local slotWidth = 86
local slotHeight = 86
local startY = 800 - 180
local spacing = -20
for i = 1, 10 do
-- 创建格子背景 - 修正参数顺序
local slotBg = GUI:Image_Create(Bag._equipmentScrollView,
"equipment_slot_bg_" .. i,
0,
startY - (i-1) * (slotHeight + spacing),
slotWidth,
slotHeight,
"res/zhuansheng2.0/4.png")
-- 创建装备图标
local equipmentIcon = GUI:Image_Create(slotBg,
"equipment_icon_" .. i,
5, 5,
slotWidth - 30,
slotHeight - 30,
"")
-- 创建装备名称文本
local equipmentName = GUI:Text_Create(slotBg,
"equipment_name_" .. i,
0, -20,
12, "#ffffff", "空槽位")
-- 添加点击事件
GUI:addOnClickEvent(slotBg, function()
Bag.OnEquipmentSlotClick(i)
end)
-- 存储格子引用
Bag._equipmentSlots[i] = {
bg = slotBg,
icon = equipmentIcon,
name = equipmentName
}
end
-- 获取装备数据
Bag.GetEquipmentData()
end
-- 修改:从服务器获取装备数据 - 使用正确的参数
function Bag.GetEquipmentData()
Bag._equipmentList = {}
-- 从服务器获取当前穿戴的装备数据
-- 注意:SL:GetMetaValue("EQUIP_DATA", equipType)的参数是装备类型,不是装备ID
local equipTypes = {0, 1, 4, 13, 16, 55} -- 装备类型:衣服、武器、头盔、斗笠、盾牌、面巾
for _, equipType in ipairs(equipTypes) do
local equipData = SL:GetMetaValue("EQUIP_DATA", equipType)
if equipData and equipData.id then
table.insert(Bag._equipmentList, {
id = equipData.id,
name = equipData.name or "未知装备",
type = equipType
})
end
end
-- 更新显示
if Bag.UpdateEquipmentDisplay then
Bag.UpdateEquipmentDisplay()
end
end
```
主要修改点:
1. `GUI:Image_Create`函数需要按照`(parent, id, x, y, width, height, imagePath)`的参数顺序
2. `UpdateEquipmentDisplay`函数中,空槽位时隐藏整个格子背景
3. `GetEquipmentData`函数使用装备类型作为参数获取数据
这样修改后,空槽位就不会显示出来了。