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:25:17 |
👁️ 阅读: 0
```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.name = "n1_chuansong"
n1_chuansong.selectedIndex = 1 -- 记录当前选中按钮索引
n1_chuansong.buttonList = {} -- 存储按钮对象
-- 创建单个按钮的函数
function n1_chuansong:createButton(index, data)
local bt = GUI:Button_Create(self.ui.ListView_1, "bt" .. index, 0, 0, "res/custom/chuansong/an1.png")
GUI:Button_setTitleText(bt, data.name)
GUI:Button_setTitleFontSize(bt, 18)
-- 设置按钮点击事件
GUI:addOnClickEvent(bt, function()
self:onButtonClick(index)
end)
-- 存储按钮引用
self.buttonList[index] = bt
-- 设置默认选中状态
if index == self.selectedIndex then
GUI:Button_setTitleColor(bt, "#FFD700") -- 金色
self.p1 = data.mapId
else
GUI:Button_setTitleColor(bt, "#A0A0A4") -- 灰色
end
return bt
end
-- 按钮点击处理函数
function n1_chuansong:onButtonClick(index)
-- 更新所有按钮颜色
for i, bt in ipairs(self.buttonList) do
if GUI:Win_IsNotNull(bt) then
if i == index then
GUI:Button_setTitleColor(bt, "#FFD700") -- 选中按钮变金色
self.selectedIndex = index
self.p1 = list[index].mapId
else
GUI:Button_setTitleColor(bt, "#A0A0A4") -- 其他按钮变灰色
end
end
end
end
-- 初始化按钮容器
function n1_chuansong:initButtons()
local list_v = self.ui.ListView_1
if list_v then
GUI:removeAllChildren(list_v)
end
-- 清空按钮列表
self.buttonList = {}
-- 创建所有按钮
for k, v in ipairs(list) do
self:createButton(k, v)
end
end
function n1_chuansong:main()
SL:print("===========进入到NPC界面")
-- 创建界面
local parent = GUI:Win_Create(self.name, 0, 0, 0, 0, false, false, true, true)
-- 加载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)
-- 点击确定按钮
self.p1 = list[1].mapId -- 默认第一个地图ID
GUI:addOnClickEvent(self.ui.btok, function()
SL:print("传送至地图ID:", self.p1)
SL:SendLuaNetMsg(110, self.p1, 0, 0, "")
end)
-- 初始化按钮容器
self:initButtons()
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. **按钮创建分离**:将按钮创建逻辑提取到`createButton`函数中,使代码更清晰
2. **状态管理**:
- 添加`selectedIndex`记录当前选中按钮
- 添加`buttonList`存储所有按钮对象
3. **点击效果**:
- `onButtonClick`函数处理按钮点击,更新所有按钮颜色
- 选中按钮显示金色(`#FFD700`),其他按钮显示灰色(`#A0A0A4`)
4. **默认选中**:界面打开时默认选中第一个按钮,文字为金色
5. **地图ID传递**:点击按钮时更新`self.p1`为对应的地图ID
这样修改后,按钮创建逻辑被分离到独立函数中,点击效果更加直观,且默认选中第一个按钮。
← 返回问答库