SL:print("==========加载传送员")
local n1_chuansong = {}
n1_chuansong.name = "n1_chuansong"
function n1_chuansong:main()
SL:print("===========进入到NPC界面")
-- 创建界面
local parent = GUI:Win_Create(self.name, 0, 0, 0, 0, false, false, false, true,
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-06 14:19:57 |
👁️ 阅读: 0
我检查了你的代码,发现有几个问题需要修正:
**主要问题:**
1. **Win_Create参数错误** - 你使用了11个参数,但根据浮生梦老师前端课程内容,`GUI:Win_Create`应该只有10个参数
2. **界面居中方式错误** - 不应该直接设置Node的位置,应该设置父节点位置
3. **缺少必要的错误处理**
**修正后的代码:**
```lua
SL:print("==========加载传送员")
local n1_chuansong = {}
n1_chuansong.name = "n1_chuansong"
function n1_chuansong:main()
SL:print("===========进入到NPC界面")
-- 创建界面 - 修正参数
local parent = GUI:Win_Create(self.name, 0, 0, 0, 0, false, false, true, false, false)
if GUI:Win_IsNull(parent) then
SL:print("创建界面失败")
return
end
self.parent = parent
-- 加载ui文件
GUI:LoadExport(parent, "A/n1_chuansong")
self.ui = GUI:ui_delegate(parent)
-- 界面居中 - 正确方式
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH", 0)
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT", 0)
GUI:setPosition(parent, screenWidth / 2, screenHeight / 2)
-- 按钮关闭界面
if not GUI:Win_IsNull(self.ui.close) then
GUI:addOnClickEvent(self.ui.close, function()
GUI:Win_Close(parent)
end)
end
-- 背景关闭界面
if not GUI:Win_IsNull(self.ui.bg_black) then
GUI:addOnClickEvent(self.ui.bg_black, function()
GUI:Win_Close(parent)
end)
end
-- 点击按钮
self.p1 = 0
if not GUI:Win_IsNull(self.ui.btok) then
GUI:addOnClickEvent(self.ui.btok, function()
-- 向服务端发送消息
SL:SendLuaNetMsg(110, self.p1, 0, 0, "")
end)
end
end
-- 接收来自服务端消息(点击NPC)
SL:RegisterLuaNetMsg(100, function(msgID, p1, p2, p3, msgData)
SL:print("收到100号消息,打开NPC界面")
n1_chuansong:main()
end)
-- 接收来自服务端消息(点击按钮后)
SL:RegisterLuaNetMsg(101, function(msgID, p1, p2, p3, msgData)
SL:print("收到101号消息:", msgID, p1, p2, p3, msgData)
-- 检查界面是否还存在
if n1_chuansong.ui and not GUI:Win_IsNull(n1_chuansong.ui.Text_1) then
GUI:Text_setString(n1_chuansong.ui.Text_1, "测试一下")
else
SL:print("界面已关闭或Text_1不存在")
end
end)
return n1_chuansong
```
**关键修改点:**
1. `GUI:Win_Create`参数从11个改为10个,最后一个参数是层级
2. 使用`GUI:Win_IsNull()`检查控件是否存在
3. 居中父节点而不是子节点
4. 获取屏幕尺寸使用`SL:GetMetaValue`
**服务端也需要检查:**
确保服务端的`sendluamsg`函数正确调用,并且客户端确实收到了100号消息。可以在客户端添加更多打印信息来调试。
如果还是打不开,检查:
1. UI文件路径是否正确
2. 服务端是否真的发送了100号消息
3. 客户端是否注册了100号消息处理函数
← 返回问答库