课程入口
- 基础NPC触发方式
-- 后端触发NPC示例
local npcID = 76
SL:print("打开NPC:"..npcID)
-- 前端接收协议示例(100号协议)
if p1 == 100 then
local npcTable = {
[76] = {name = "合成系统", item = "木剑1"},
[77] = {name = "强化系统", item = "木剑2"},
[78] = {name = "升级系统", item = "木剑3"}
}
local currentNPC = npcTable[tonumber(p2)]
if currentNPC then
-- 创建界面逻辑
end
end
- 前端界面创建流程
-- 1. 创建父节点
local parent = GUI:Win_Create("NPCWindow", 0, 0, 1136, 640, false, false, true, false, false, false, 1)
-- 2. 加载UI文件
GUI:LoadExport(parent, "game_npc_window")
-- 3. 获取子控件
local ui = GUI:ui_delegate(parent)
if ui then
-- 设置文本内容
GUI:setText(ui.Text_desc, currentNPC.name)
-- 设置按钮事件
GUI:addOnClickEvent(ui.Button_close, function()
GUI:Win_Close(parent)
end)
end
- 重要知识点总结:
- 协议通信:后端通过100号协议触发前端界面
- 组件封装:建议在前端进行UI组件封装
- 自适应布局:使用GUI:setAnchorPoint()设置锚点
- 节点操作:通过GUI:getChildByName()获取子节点
- 数据传递:用table结构管理NPC数据
- 常见问题解决方案:
-- 乱码问题:确保文件编码为UTF-8
-- 节点获取不到:检查GUI:Win_IsNotNull()判断
-- 协议不触发:检查协议号是否冲突
-- 位置异常:使用GUI:setPosition()调整坐标
- 最佳实践建议:
- 使用SL:ScheduleOnce()处理延迟操作
- 重要操作前用GUI:Win_IsNull()做空判断
- 复杂界面建议分模块开发
- PC端注意GUI:setSwallowRightMouseTouch()