你的代码逻辑存在一个问题:`pageButtons[1]` 实际上是一个空表 `{}`,你虽然初始化了它,但从未向其中添加按钮对象。因此 `firstBtn` 永远是 `nil`,导致自动创建第一个套装内容的逻辑无法执行。
修复方法:在创建按钮时,将其存入 `pageButtons[page]` 表中。
以下是修正后的关键代码部分:
```lua
-- 在创建按钮的循环内,添加以下代码
local btnName = "btn" .. k
local btn_Item = GUI:Button_Create(IG_turnPage, btnName, ly_x, ly_y, "res/01/010006.png")
-- ... 其他按钮设置代码 ...
-- 将按钮对象存入 pageButtons 表
table.insert(pageButtons[page], btn_Item)
-- ... 按钮点击事件代码 ...
```
然后修改自动创建第一个套装内容的逻辑:
```lua
-- 自动创建第一个套装的内容
if pageButtons[1] and #pageButtons[1] > 0 then
local firstBtn = pageButtons[1][1]
if firstBtn and not GUI:Win_IsNull(firstBtn) then
-- 模拟点击第一个按钮来创建内容
local clickFunc = GUI:getOnClickEvent(firstBtn)
if clickFunc then
clickFunc()
end
end
end
```
或者更直接的方式,在页面创建完成后直接调用第一个按钮的点击处理逻辑:
```lua
-- 在页面创建完成后,直接创建第一个套装的内容
local itemsConfig = npc.config[1].itemsIdx
local itemName = npc.config[1].itemsName
local imgStart_x = 10
local imgStart_y = 200
for i = 1, #itemsConfig do
local itemIdx = itemsConfig[i]
local name = itemName[i]
if itemIdx then
local img_x = (i - 1) % 3 * 162 + imgStart_x
local img_y = imgStart_y - math.floor((i-1)/3) * 160
local itemShowBg = "itemShowBg"..i
local itemShow = "itemShow"..i
local nameText = "nameText"..i
local stateText = "stateTxext"..i
local itemShow_bg = GUI:Image_Create(npc.ui.content_Layout, itemShowBg, img_x, img_y, "res/public/1900000610799.png")
GUI:setContentSize(itemShow_bg, 165, 150)
local itemShowOut = GUI:ItemShow_Create(itemShow_bg, itemShow, 63, 100, {index = itemIdx, count = 1, look = true, bgVisible = true, color = 255})
local centerX = 80 - string.len(name)*2.8
local itemName_Txt = GUI:Text_Create(itemShow_bg, nameText, centerX, 45, 16, "#ffffff", name)
local state_Txt = GUI:Text_Create(itemShow_bg, stateText, 60, 10, 16, "#ffffff", "0 / 1")
table.insert(layoutRefs, itemShow_bg)
end
end
```
这样修改后,进入页面时就会自动显示第一个按钮对应的套装内容了。