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 12:44:06 |
👁️ 阅读: 6
根据你的需求,在背包右侧添加一个垂直滚动容器来显示装备,并自动创建10个格子。以下是完整的代码添加部分,不修改官方原有代码:
```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.CreateEquipmentScrollView()
-- 初始化左侧背包页签
Bag.InitPage()
Bag.PageTo(page or 1)
Bag.OnUpdateGold()
Bag.RegisterEvent()
end
-- 创建装备滚动容器函数
function Bag.CreateEquipmentScrollView()
-- 创建垂直滚动容器
Bag._equipScrollView = GUI:ScrollView_Create(Bag._parent, "EquipScrollView", 0, 0, 80, 392)
-- 设置滚动方向为垂直
GUI:ScrollView_setDirection(Bag._equipScrollView, 1)
-- 设置容器位置(在背包右侧)
local bagPos = GUI:getPosition(Bag._ui["Panel_1"])
GUI:setPosition(Bag._equipScrollView, bagPos.x + Bag._PWidth + 20, bagPos.y)
-- 设置容器背景(可选)
GUI:ScrollView_setBackGroundColor(Bag._equipScrollView, "#000000")
GUI:ScrollView_setBackGroundColorOpacity(Bag._equipScrollView, 100)
-- 创建10个装备格子
Bag.CreateEquipmentSlots()
end
-- 创建10个装备格子函数
function Bag.CreateEquipmentSlots()
Bag._equipSlots = {}
local slotWidth = 70
local slotHeight = 70
local spacing = 5
local totalHeight = 0
-- 装备列表
local equipmentList = {
"九天玄气",
"阴阳逆乱",
"乾坤定星盘",
"鬼新娘の呼唤",
"帝乙木敕令",
"无极剑魂·道天一心",
"装备7",
"装备8",
"装备9",
"装备10"
}
for i = 1, 10 do
-- 创建格子背景
local slotBg = GUI:Image_Create(Bag._equipScrollView, "EquipSlotBg_" .. i, 5, totalHeight, "res/public/1900000610.png")
GUI:setContentSize(slotBg, slotWidth, slotHeight)
-- 创建装备图标
local equipIcon = GUI:Image_Create(slotBg, "EquipIcon_" .. i, 0, 0, "res/public/1900000612.png")
GUI:setPosition(equipIcon, slotWidth/2, slotHeight/2)
GUI:setAnchorPoint(equipIcon, 0.5, 0.5)
-- 创建装备名称文本
local equipName = GUI:Text_Create(slotBg, "EquipName_" .. i, 0, -25, 12, "#ffffff", equipmentList[i])
GUI:setAnchorPoint(equipName, 0.5, 0.5)
GUI:setPosition(equipName, slotWidth/2, slotHeight/2)
GUI:Text_setFontName(equipName, "fonts/font5.ttf")
-- 添加点击事件
GUI:addOnClickEvent(slotBg, function()
SL:print("点击了装备:" .. equipmentList[i])
end)
-- 存储格子引用
Bag._equipSlots[i] = {
bg = slotBg,
icon = equipIcon,
name = equipName
}
totalHeight = totalHeight + slotHeight + spacing
end
-- 设置滚动容器内部大小
GUI:ScrollView_setInnerContainerSize(Bag._equipScrollView, 80, totalHeight)
end
-- 在Bag.OnClose函数中清理资源
function Bag.OnClose(winID)
if winID and winID == "BagLayerGUI" then
Bag.UnRegisterEvent()
-- 清理装备容器
if Bag._equipScrollView and not GUI:Win_IsNull(Bag._equipScrollView) then
GUI:removeFromParent(Bag._equipScrollView)
Bag._equipScrollView = nil
end
Bag._equipSlots = nil
end
end
```
这段代码添加了以下功能:
1. **创建垂直滚动容器**:宽80,高392,位于背包右侧
2. **自动创建10个装备格子**:每个格子包含背景、图标和装备名称
3. **装备列表**:包含你提到的6个装备名称,后4个用占位符
4. **点击事件**:点击每个格子会打印装备名称
5. **资源清理**:关闭界面时自动清理创建的组件
注意:代码中使用了`res/public/1900000610.png`作为格子背景,`res/public/1900000612.png`作为装备图标,你可以根据需要替换为实际的资源路径。
根据你的需求,在背包右侧添加一个垂直滚动容器来显示装备,并自动创建10个格子。以下是完整的代码添加部分,不修改官方原有代码:
```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.CreateEquipmentScrollView()
-- 初始化左侧背包页签
Bag.InitPage()
Bag.PageTo(page or 1)
Bag.OnUpdateGold()
Bag.RegisterEvent()
end
-- 创建装备滚动容器函数
function Bag.CreateEquipmentScrollView()
-- 创建垂直滚动容器
Bag._equipScrollView = GUI:ScrollView_Create(Bag._parent, "EquipScrollView", 0, 0, 80, 392)
-- 设置滚动方向为垂直
GUI:ScrollView_setDirection(Bag._equipScrollView, 1)
-- 设置容器位置(在背包右侧)
local bagPos = GUI:getPosition(Bag._ui["Panel_1"])
GUI:setPosition(Bag._equipScrollView, bagPos.x + Bag._PWidth + 20, bagPos.y)
-- 设置容器背景(可选)
GUI:ScrollView_setBackGroundColor(Bag._equipScrollView, "#000000")
GUI:ScrollView_setBackGroundColorOpacity(Bag._equipScrollView, 100)
-- 创建10个装备格子
Bag.CreateEquipmentSlots()
end
-- 创建10个装备格子函数
function Bag.CreateEquipmentSlots()
Bag._equipSlots = {}
local slotWidth = 70
local slotHeight = 70
local spacing = 5
local totalHeight = 0
-- 装备列表
local equipmentList = {
"九天玄气",
"阴阳逆乱",
"乾坤定星盘",
"鬼新娘の呼唤",
"帝乙木敕令",
"无极剑魂·道天一心",
"装备7",
"装备8",
"装备9",
"装备10"
}
for i = 1, 10 do
-- 创建格子背景
local slotBg = GUI:Image_Create(Bag._equipScrollView, "EquipSlotBg_" .. i, 5, totalHeight, "res/public/1900000610.png")
GUI:setContentSize(slotBg, slotWidth, slotHeight)
-- 创建装备图标
local equipIcon = GUI:Image_Create(slotBg, "EquipIcon_" .. i, 0, 0, "res/public/1900000612.png")
GUI:setPosition(equipIcon, slotWidth/2, slotHeight/2)
GUI:setAnchorPoint(equipIcon, 0.5, 0.5)
-- 创建装备名称文本
local equipName = GUI:Text_Create(slotBg, "EquipName_" .. i, 0, -25, 12, "#ffffff", equipmentList[i])
GUI:setAnchorPoint(equipName, 0.5, 0.5)
GUI:setPosition(equipName, slotWidth/2, slotHeight/2)
GUI:Text_setFontName(equipName, "fonts/font5.ttf")
-- 添加点击事件
GUI:addOnClickEvent(slotBg, function()
SL:print("点击了装备:" .. equipmentList[i])
end)
-- 存储格子引用
Bag._equipSlots[i] = {
bg = slotBg,
icon = equipIcon,
name = equipName
}
totalHeight = totalHeight + slotHeight + spacing
end
-- 设置滚动容器内部大小
GUI:ScrollView_setInnerContainerSize(Bag._equipScrollView, 80, totalHeight)
end
-- 在Bag.OnClose函数中清理资源
function Bag.OnClose(winID)
if winID and winID == "BagLayerGUI" then
Bag.UnRegisterEvent()
-- 清理装备容器
if Bag._equipScrollView and not GUI:Win_IsNull(Bag._equipScrollView) then
GUI:removeFromParent(Bag._equipScrollView)
Bag._equipScrollView = nil
end
Bag._equipSlots = nil
end
end
```
这段代码添加了以下功能:
1. **创建垂直滚动容器**:宽80,高392,位于背包右侧
2. **自动创建10个装备格子**:每个格子包含背景、图标和装备名称
3. **装备列表**:包含你提到的6个装备名称,后4个用占位符
4. **点击事件**:点击每个格子会打印装备名称
5. **资源清理**:关闭界面时自动清理创建的组件
注意:代码中使用了`res/public/1900000610.png`作为格子背景,`res/public/1900000612.png`作为装备图标,你可以根据需要替换为实际的资源路径。
← 返回问答库