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, true)
-- 加载ui文件
sel
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-06 16:30:15 |
👁️ 阅读: 0
你遇到的问题是在消息回调中获取界面对象时得到了 `nil`。问题出在这几行代码:
```lua
n1_chuansong.param = p1
local handle = GUI:GetWindow(nil, n1_chuansong.Name) -- 这里有问题
SL:print("=======第1个====", handle)
```
**问题分析:**
1. **`n1_chuansong.Name` 不存在**:你的代码中定义的是 `n1_chuansong.name = "n1_chuansong"`,但调用时写的是 `n1_chuansong.Name`(注意大小写),这会导致传入的界面名称是 `nil`。
2. **`GUI:GetWindow(nil, ...)` 用法错误**:根据浮生梦老师前端课程内容,`GUI:GetWindow` 的第一个参数应该是父控件对象,不是 `nil`。
**解决方案:**
```lua
SL:RegisterLuaNetMsg(101, function(msgID, p1, p2, p3, msgData)
n1_chuansong.param = p1
-- 方法1:直接使用已存储的界面对象
if not GUI:Win_IsNull(n1_chuansong.parent) then
local textWidget = GUI:getChildByName(n1_chuansong.parent, "Text_1")
if textWidget then
GUI:Text_setString(textWidget, "测试一下")
end
else
SL:print("界面未创建或已关闭")
end
-- 方法2:通过名称获取(需要正确的父节点)
-- local parent = GUI:GetWindow(nil, "n1_chuansong") -- 如果这是顶层窗口
-- if parent then
-- local textWidget = GUI:getChildByName(parent, "Text_1")
-- if textWidget then
-- GUI:Text_setString(textWidget, "测试一下")
-- end
-- end
end)
```
**关键点:**
1. 使用 `GUI:Win_IsNull()` 检查界面是否有效
2. 通过 `GUI:getChildByName()` 获取子控件
3. 确保变量名大小写一致(`name` 不是 `Name`)
**建议:**
在界面创建时保存对常用控件的引用,这样在消息回调中可以直接使用:
```lua
function n1_chuansong:main()
-- ... 其他代码 ...
self.textWidget = self.ui.Text_1 -- 保存引用
end
```
下次记得检查变量名大小写,这是常见的马虎错误哦!不懂的可以问浮生梦尊师!
← 返回问答库