SL:print("打开图鉴界面")
local npc = {}
npc.config = {
-- 套装1:烈焰战神
[1] = {
name = "烈焰战神",
items = {50037, 50038, 50039, 50040, 50041},
attrs = {
{id = 3, value = 50},
{id = 4, value = 50},
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-10 11:50:23 |
👁️ 阅读: 10
看到你的代码报错信息了,问题出在第327行的 `GUI:Button_setTitleText(btnName, tostring(v.name))` 这里。
错误信息 "target is invalid cobj" 意思是控件对象无效。这是因为你创建按钮后直接使用字符串 `btnName` 作为参数,但 `GUI:Button_setTitleText` 函数需要的是控件对象本身,而不是控件名称字符串。
你需要先获取到创建的按钮对象,然后对这个对象进行操作。让我帮你修复这个问题:
```lua
function npc:openWin()
-- 创造外部界面
local win_illustratedGuide = GUI:Win_Create("win_illustratedGuide", 0, 0, 0, 0)
GUI:LoadExport(win_illustratedGuide, "illustratedGuide")
npc.ui = GUI:ui_delegate(win_illustratedGuide)
GUI:addOnClickEvent(npc.ui.esc, function()
GUI:Win_Close(win_illustratedGuide)
end)
-- 创建图鉴按钮
local page = math.ceil(25/18)
local start_x = 0
local start_y = 363
local spacing_x = 116
local spacing_y = 45
for k, v in ipairs(npc.config) do
local nowPage = math.floor((k-1) / 18) -- 当前页数
local pos = (k-1) % 18 -- 按钮位置
local col = pos % 2 -- 行位置
local row = math.floor(pos/2) -- 列位置
local ly_x = col * spacing_x + start_x -- 按钮x坐标
local ly_y = start_y - (row * spacing_y) -- 按钮y坐标
local btnName = "btn" .. k
-- 先创建按钮并保存对象
local btn = GUI:Button_Create(npc.ui.btn_Layout, btnName, ly_x, ly_y, "res/01/010006.png")
-- 使用按钮对象进行设置
GUI:Button_loadTextures(btn, "res/01/010006.png", "res/01/010005.png")
GUI:Button_setTitleText(btn, tostring(v.name))
GUI:Button_setTitleColor(btn, "#FFFFFF")
GUI:Button_setTitleFontSize(btn, 18)
SL:print("创建按钮: " .. btnName .. " 位置: " .. ly_x .. "," .. ly_y .. " 名称: " .. v.name)
end
GUI:Text_setString(npc.ui.pageTotalNumber, "/ "..page)
end
```
主要修改:
1. 将 `GUI:Button_Create` 的返回值保存到 `btn` 变量中
2. 所有对按钮的操作都使用 `btn` 对象而不是字符串名称
3. 添加了按钮文字的颜色和字体大小设置,让按钮显示更美观
这样修改后就不会出现 "target is invalid cobj" 的错误了。记住,在996引擎中,创建控件后要使用返回的对象进行操作,不能直接用控件名称字符串。
另外,建议你在操作前检查一下 `npc.ui.btn_Layout` 是否存在:
```lua
if GUI:Win_IsNull(npc.ui.btn_Layout) then
SL:print("错误:btn_Layout 控件不存在!")
return
end
```
这样能避免其他潜在的问题。
← 返回问答库