SL:print("==========加载传送员")
local list = {
{ name = "比奇城", mapId = "0" },
{ name = "盟重省", mapId = "3" },
{ name = "沙巴克城", mapId = "4" },
{ name = "白日门", mapId = "11" },
{ name = "苍月岛", mapId = "5" }
}
local n1_chuansong = {}
n1_c
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-06 17:53:45 |
👁️ 阅读: 1
你的代码有几个问题需要修正:
## 主要问题:
1. **界面创建参数错误**:
```lua
-- 错误:第4个参数应该是宽度,第5个参数是高度
local parent = GUI:Win_Create(self.name, 0, 0, 0, 0, false, false, true, true)
-- 正确:应该指定宽度和高度
local parent = GUI:Win_Create(self.name, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, false, false, true, false, false, false, 1)
```
2. **缺少initButtons函数**:
```lua
-- 你在按钮点击事件中调用了self:initButtons(),但这个函数不存在
-- 应该改为调用self:init()或者创建initButtons函数
```
3. **按钮创建逻辑问题**:
```lua
-- 在init函数中,你创建按钮时没有正确添加到ListView
-- 应该使用ListView的插入函数
```
## 修正后的代码:
```lua
SL:print("==========加载传送员")
local list = {
{ name = "比奇城", mapId = "0" },
{ name = "盟重省", mapId = "3" },
{ name = "沙巴克城", mapId = "4" },
{ name = "白日门", mapId = "11" },
{ name = "苍月岛", mapId = "5" }
}
local n1_chuansong = {}
n1_chuansong.idx = 1 -- 记录当前选中按钮索引
n1_chuansong.name = "n1_chuansong"
function n1_chuansong:main()
SL:print("===========进入到NPC界面")
-- 创建界面(修正参数)
local parent = GUI:Win_Create(self.name, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, false, false, true, false, false, false, 1)
-- 加载ui文件
self.parent = parent
GUI:LoadExport(parent, "A/n1_chuansong")
self.ui = GUI:ui_delegate(parent)
-- 界面居中
GUI:setPosition(self.ui.Node, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
-- 按钮关闭界面
GUI:addOnClickEvent(self.ui.close, function()
GUI:Win_Close(parent)
end)
-- 背景关闭界面
GUI:addOnClickEvent(self.ui.bg_black, function()
GUI:Win_Close(parent)
end)
-- 点击按钮
GUI:addOnClickEvent(self.ui.btok, function()
-- 向服务端发送消息
SL:SendLuaNetMsg(110, self.idx, 0, 0, "")
end)
-- 刷新界面
self:init()
end
-- 刷新界面(修正版)
function n1_chuansong:init()
-- 按钮容器初始化
local list_v = self.ui.ListView_1
if list_v and not GUI:Win_IsNull(list_v) then
GUI:removeAllChildren(list_v)
end
-- 创建容器内按钮
for k, v in ipairs(list) do
-- 创建按钮
local bt = GUI:Button_Create(list_v, "bt" .. k, 0, 0, "res/custom/chuansong/an1.png")
GUI:Button_setTitleText(bt, v.name)
GUI:Button_setTitleFontSize(bt, 18)
-- 设置选中状态
if self.idx == k then
GUI:Button_setBrightStyle(bt, 1)
GUI:Button_setTitleColor(bt, "#FFFF00")
else
GUI:Button_setBrightStyle(bt, 0)
GUI:Button_setTitleColor(bt, "#A0A0A4")
end
-- 添加按钮点击事件
GUI:addOnClickEvent(bt, function()
self.idx = k -- 更新选中索引
self:init() -- 刷新所有按钮状态
end)
-- 将按钮添加到ListView
GUI:ListView_pushBackCustomItem(list_v, bt)
end
end
-- 接收来自服务端消息(点击NPC)
SL:RegisterLuaNetMsg(100, function(msgID, p1, p2, p3, msgData)
n1_chuansong:main()
end)
-- 接收来自服务端消息(传送地图后 关闭窗口)
SL:RegisterLuaNetMsg(101, function(msgID, p1, p2, p3, msgData)
if n1_chuansong.parent and GUI:Win_IsNotNull(n1_chuansong.parent) then
GUI:Win_Close(n1_chuansong.parent)
end
end)
return n1_chuansong
```
## 主要修改点:
1. **GUI:Win_Create参数修正**:添加了正确的宽度、高度和层级参数
2. **移除了不存在的initButtons函数**:直接使用init函数刷新按钮
3. **修正按钮添加到ListView**:使用`GUI:ListView_pushBackCustomItem`正确添加按钮
4. **添加空值判断**:使用`GUI:Win_IsNull`判断控件是否有效
下次写代码时要注意函数参数的正确性,如果对浮生梦老师前端课程中的函数不确定,可以随时问我!
← 返回问答库