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
根据浮生梦老师前端课程内容,要在背包右侧的滚动容器中创建10个装备格子并自动放置装备,你需要先创建一个滚动容器(比如ScrollView),然后在里面创建10个格子。每个格子可以用Widget来承载装备信息。
以下是完整的代码实现:
```lua
-- 在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")
Bag._ui = GUI:ui_delegate(parent)
Bag._UI_ScrollView = Bag._ui["ScrollView_items"]
-- 初始化数据
Bag.Init(isWin32)
-- 适配
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)
-- 存入英雄背包
local Button_store_hero_bag = Bag._ui["Button_store_hero_bag"]
GUI:addOnClickEvent(Button_store_hero_bag, function()
local changeStoreMode = not Bag._changeStoreMode
if changeStoreMode then
local isActiveHero = SL:GetMetaValue("HERO_IS_ACTIVE")
if not isActiveHero then
return SL:ShowSystemTips("英雄还未激活")
end
local isCallHero = SL:GetMetaValue("HERO_IS_ALIVE")
if not isCallHero then
return SL:ShowSystemTips("英雄还未召唤")
end
end
Bag._changeStoreMode = changeStoreMode
GUI:Button_setGrey(Button_store_hero_bag, changeStoreMode)
end)
GUI:setVisible(Button_store_hero_bag, SL:GetMetaValue("USEHERO"))
-- 初始化左侧背包页签
Bag.InitPage()
Bag.PageTo(page or 1)
Bag.OnUpdateGold()
-- 初始化右侧装备滚动容器
Bag.InitEquipScrollView()
Bag.RegisterEvent()
end
-- 新增函数:初始化装备滚动容器
function Bag.InitEquipScrollView()
-- 假设你的滚动容器在UI中的名字是"ScrollView_equip"
Bag._equipScrollView = Bag._ui["ScrollView_equip"]
if not Bag._equipScrollView then
SL:Print("警告:未找到ScrollView_equip控件")
return
end
-- 设置滚动容器属性
GUI:ScrollView_setDirection(Bag._equipScrollView, 1) -- 1表示垂直滚动
-- 设置内部容器大小(10个格子,每个格子高100)
local containerHeight = 10 * 100
local containerWidth = GUI:getContentSize(Bag._equipScrollView).width
GUI:ScrollView_setInnerContainerSize(Bag._equipScrollView, containerWidth, containerHeight)
-- 创建10个装备格子
Bag._equipSlots = {}
local equipNames = {
"九天玄气",
"阴阳逆乱",
"乾坤定星盘",
"鬼新娘の呼唤",
"帝乙木敕令",
"无极剑魂·道天一心"
}
for i = 1, 10 do
-- 创建格子背景
local slotX = 10
local slotY = containerHeight - (i * 100) + 20 -- 从顶部开始排列
local slotWidget = GUI:Widget_Create(Bag._equipScrollView, "equip_slot_" .. i, slotX, slotY, 80, 80)
-- 创建格子背景图片
local bgImage = GUI:Image_Create(slotWidget, "bg_" .. i, 0, 0, "res/public/1900000612.png")
GUI:setContentSize(bgImage, 80, 80)
-- 创建装备图标(如果有对应的装备)
local equipName = equipNames[i]
if equipName then
local equipIcon = GUI:Image_Create(slotWidget, "icon_" .. i, 10, 10, "res/public/1900000610.png")
GUI:setContentSize(equipIcon, 60, 60)
-- 创建装备名字文本
local nameText = GUI:Text_Create(slotWidget, "name_" .. i, 0, -20, 12, "#ffffff", equipName)
GUI:setAnchorPoint(nameText, 0.5, 0.5)
GUI:setPositionX(nameText, 40)
end
-- 添加点击事件
GUI:addOnClickEvent(slotWidget, function()
SL:Print("点击了装备格子" .. i)
if equipNames[i] then
SL:ShowSystemTips("选中装备:" .. equipNames[i])
end
end)
-- 存储格子引用
Bag._equipSlots[i] = slotWidget
end
end
-- 新增函数:更新装备格子
function Bag.UpdateEquipSlot(index, equipData)
if not Bag._equipSlots or not Bag._equipSlots[index] then
return
end
local slotWidget = Bag._equipSlots[index]
-- 清除旧的装备图标
local oldIcon = GUI:getChildByName(slotWidget, "icon_" .. index)
if oldIcon then
GUI:removeFromParent(oldIcon)
end
-- 清除旧的名字文本
local oldName = GUI:getChildByName(slotWidget, "name_" .. index)
if oldName then
GUI:removeFromParent(oldName)
end
-- 如果有新的装备数据,创建新的显示
if equipData and equipData.name then
local equipIcon = GUI:Image_Create(slotWidget, "icon_" .. index, 10, 10, "res/public/1900000610.png")
GUI:setContentSize(equipIcon, 60, 60)
local nameText = GUI:Text_Create(slotWidget, "name_" .. index, 0, -20, 12, "#ffffff", equipData.name)
GUI:setAnchorPoint(nameText, 0.5, 0.5)
GUI:setPositionX(nameText, 40)
end
end
-- 在Bag.RegisterEvent函数中注册装备更新事件
function Bag.RegisterEvent()
SL:RegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "Bag", Bag.OnUpdateGold)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "Bag", Bag.OnClose)
-- 注册装备更新事件(假设有装备更新事件)
SL:RegisterLUAEvent("EQUIP_UPDATE", "Bag", Bag.OnEquipUpdate)
end
function Bag.UnRegisterEvent()
SL:UnRegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "Bag")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "Bag")
SL:UnRegisterLUAEvent("EQUIP_UPDATE", "Bag")
end
-- 新增函数:处理装备更新
function Bag.OnEquipUpdate(data)
if data and data.index and data.equipData then
Bag.UpdateEquipSlot(data.index, data.equipData)
end
end
```
**代码说明:**
1. **初始化滚动容器**:在`Bag.InitEquipScrollView`函数中,首先获取滚动容器控件,设置垂直滚动方向,并计算内部容器大小。
2. **创建10个格子**:使用循环创建10个装备格子,每个格子包含:
- 背景图片(使用你指定的按钮图片)
- 装备图标(使用你指定的背景图片作为图标占位)
- 装备名称文本
- 点击事件
3. **装备名称数组**:我创建了一个包含6个装备名称的数组,前6个格子会显示这些装备名称,后4个格子为空。
4. **更新功能**:提供了`UpdateEquipSlot`函数,可以动态更新指定格子的装备显示。
5. **事件注册**:注册了装备更新事件,当服务端发送装备更新时,可以自动刷新格子显示。
**使用说明:**
1. 确保你的GUI布局中有一个名为`ScrollView_equip`的滚动容器控件
2. 代码会自动创建10个装备格子,并显示指定的装备名称
3. 点击格子会打印日志并显示提示
4. 可以通过调用`Bag.UpdateEquipSlot(index, equipData)`来动态更新装备显示
如果你需要从服务端获取装备数据,可以使用`SL:GetMetaValue`函数获取,然后在`Bag.main`函数中初始化时调用更新函数。