function npc:bag_item_show(p1, p2)
local ListView_2 = self.ui.ListView_2
GUI:setVisible(ListView_2, true)
GUI:setVisible(self.ui.item_colse, true)
local item_bg = self.ui.item
GUI:ListView_removeAllItems(ListView_2)
-- 获取背包石头
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-31 12:07:19 |
👁️ 阅读: 2
根据浮生梦老师前端课程内容,我来帮你封装一个更规范的函数。主要改进点包括:
1. 使用正确的创建函数而不是克隆
2. 明确创建流程
3. 优化事件处理
4. 使用正确的定时器函数
```lua
function createBagItemUI(parent, bagItems, p1, p2)
-- 创建主容器
local mainLayout = GUI:Layout_Create(parent, "bag_item_layout", 0, 0, 400, 500, false)
-- 创建关闭按钮
local closeBtn = GUI:Button_Create(mainLayout, "item_colse", 350, 10, "res/public_win32/1900000612.png")
GUI:Button_setTitleText(closeBtn, "关闭")
GUI:addOnClickEvent(closeBtn, function()
GUI:setVisible(mainLayout, false)
end)
-- 创建列表容器
local listView = GUI:ListView_Create(mainLayout, "ListView_2", 10, 50, 380, 440, 1, 380, 140, 3)
GUI:setVisible(listView, true)
-- 获取背包数据
local pageCount = math.max(math.ceil(#bagItems / 9), 3)
-- 创建分页
for i = 1, pageCount do
local pageNode = GUI:Layout_Create(nil, "item_node_" .. i, 0, 0, 380, 140, false)
GUI:setVisible(pageNode, true)
-- 创建9个物品格子
for j = 1, 9 do
local posX = ((j - 1) % 3) * 120 + 20
local posY = math.floor((j - 1) / 3) * 120 + 10
-- 背景图片
local itemBg = GUI:Image_Create(pageNode, "item_bg_" .. j, posX, posY, "res/public_win32/1900000610.png")
-- 点击区域
local clickArea = GUI:Layout_Create(itemBg, "item_click_" .. j, 0, 0, 100, 100, false)
GUI:setTouchEnabled(clickArea, true)
GUI:setSwallowTouches(clickArea, false)
end
GUI:ListView_pushBackCustomItem(listView, pageNode)
end
-- 填充物品数据
local operMode = SL:GetMetaValue("CURRENT_OPERMODE")
for idx, item in ipairs(bagItems) do
local page = math.ceil(idx / 9)
local posInPage = (idx - 1) % 9 + 1
local pageNode = GUI:getChildByName(listView, "item_node_" .. page)
if not GUI:Win_IsNull(pageNode) then
local itemBg = GUI:getChildByName(pageNode, "item_bg_" .. posInPage)
if not GUI:Win_IsNull(itemBg) then
-- 创建物品显示
local itemData = {
index = item.Index,
look = true,
bgVisible = false,
count = item.OverLap or 1,
color = item.Color or 0,
}
local offsetX = operMode == 1 and 13 or 0
local offsetY = operMode == 1 and 13 or 1
local itemShow = GUI:ItemShow_Create(itemBg, "item_show_" .. posInPage, offsetX, offsetY, itemData)
-- 获取点击区域并设置事件
local clickArea = GUI:getChildByName(itemBg, "item_click_" .. posInPage)
if not GUI:Win_IsNull(clickArea) then
GUI:addOnClickEvent(clickArea, function()
local tab = {
MakeIndex = item.MakeIndex,
id = p1,
Index = p2,
}
-- 按钮点击效果
fangzhi_liandian(clickArea, 0.3)
-- 提交表单
GUIUtil:Lua_SubmitForm("星阵", "Xz_qiangxian",
tab.MakeIndex .. "#" .. tab.id .. "#" .. tab.Index)
-- 延迟更新界面
SL:ScheduleOnce(function()
-- 更新接口函数需要你自己实现
-- npc:upde_interface(tab.id)
-- self:colse_bagitem()
-- self:upde_interface(p1)
SL:print("物品选择完成,准备更新界面")
end, 0.3)
end)
end
end
end
end
return mainLayout
end
-- 使用示例:
-- local bagItems = npc.bag_item -- 你的背包数据
-- local parent = GUI:Win_Create("BagWindow", 100, 100, 420, 520, false, false, true, false, false, false, 1)
-- local bagUI = createBagItemUI(parent, bagItems, param1, param2)
-- GUI:setVisible(parent, true)
```
**主要改进:**
1. **创建代替克隆**:使用`GUI:Layout_Create`和`GUI:Image_Create`创建控件,而不是克隆
2. **结构清晰**:每个页面都有明确的布局和位置计算
3. **事件处理**:点击事件绑定在专门的点击区域上
4. **空值检查**:使用`GUI:Win_IsNull()`检查控件是否存在
5. **定时器规范**:使用`SL:ScheduleOnce`进行延迟调用
**注意:**
- `fangzhi_liandian`和`GUIUtil:Lua_SubmitForm`函数需要你自己确认是否存在
- 更新界面的函数`upde_interface`和`colse_bagitem`需要你自己实现
- 记得创建父窗口后再调用这个函数
这样封装后,代码更加模块化,易于维护和重用!
← 返回问答库