SL:print("打开沙城捐献界面")
local npc = {}
local function networkCB(msgID, p1, p2, p3, msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-01 02:41:38 |
👁️ 阅读: 6
你的代码有几个问题需要修正:
1. **TextInput创建问题**:你在点击按钮后才创建输入框,但后面又立即尝试获取输入框对象,这时输入框可能还没创建好
2. **变量作用域问题**:`aahhh`变量在函数内部使用但未定义
3. **输入框事件注册时机**:应该在创建输入框后立即注册事件
修正后的代码:
```lua
SL:print("打开沙城捐献界面")
local npc = {}
local function networkCB(msgID, p1, p2, p3, msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
SL:print("参数3: " .. tostring(p3))
SL:print("字符串: " .. tostring(msgData))
SL:print("=======================")
if msgData == "三级界面" then
if npc.openThird then
npc:openThird()
end
end
if msgID == 1017 and msgData == "沙城捐献" then
if p3 == 0 then
if npc.openWin then
npc:openWin()
end
end
end
end
function npc:openWin()
-- 创建外部界面
local win_donate = GUI:Win_Create("win_donate", 0, 0, 0, 0, false, false, true, false, false, false, 1)
GUI:LoadExport(win_donate, "donate")
npc.ui = GUI:ui_delegate(win_donate)
-- 关闭按钮
GUI:addOnClickEvent(npc.ui.esc, function()
GUI:Win_Close(win_donate)
end)
-- 设置问号悬停提示
GUI:addMouseOverTips(npc.ui.hint_btn, "str", {x = 0, y = -55}, {x = 0.5, y = 0.5}, nil)
-- 初始化界面状态
GUI:setVisible(npc.ui.firstBg, true)
GUI:setVisible(npc.ui.secondBg, false)
GUI:setVisible(npc.ui.thirdBg, false)
-- 创建输入框(提前创建)
local inputWidget = GUI:TextInput_Create(npc.ui["inPut_bg"], "inPut", 0, 0, 220, 40, 16)
-- 设置输入框事件
GUI:TextInput_addOnEvent(inputWidget, function(sender, eventType)
if eventType == 1 then
-- 结束输入
local text = GUI:TextInput_getString(sender)
SL:print("输入完成:", text)
npc.inputValue = text
elseif eventType == 2 or eventType == 3 then
-- 内容变化
local text = GUI:TextInput_getString(sender)
SL:print("正在输入:", text)
end
end)
-- 捐献按钮点击
GUI:addOnClickEvent(npc.ui.donate_btn, function()
GUI:setVisible(npc.ui.firstBg, false)
GUI:setVisible(npc.ui.secondBg, true)
GUI:setVisible(npc.ui.thirdBg, false)
end)
-- 二级确定按钮
GUI:addOnClickEvent(npc.ui.inPut_btn1, function()
if not npc.inputValue or npc.inputValue == "" then
SL:print("请输入金额")
return
end
-- 创建确认文本
local confirmText = GUI:Text_Create(npc.ui.thirdBg, "makeSure_text", 20, 125, 16, "#FFFF00",
"您确定要捐献" .. npc.inputValue .. "个金币吗")
GUI:setVisible(npc.ui.firstBg, false)
GUI:setVisible(npc.ui.secondBg, false)
GUI:setVisible(npc.ui.thirdBg, true)
end)
-- 二级取消按钮
GUI:addOnClickEvent(npc.ui.inPut_btn2, function()
GUI:setVisible(npc.ui.firstBg, true)
GUI:setVisible(npc.ui.secondBg, false)
GUI:setVisible(npc.ui.thirdBg, false)
end)
-- 三级确定按钮
GUI:addOnClickEvent(npc.ui.makeSure_btn1, function()
-- 这里应该发送捐献请求到后端
SL:print("确认捐献:", npc.inputValue)
GUI:setVisible(npc.ui.firstBg, true)
GUI:setVisible(npc.ui.secondBg, false)
GUI:setVisible(npc.ui.thirdBg, false)
end)
-- 三级取消按钮
GUI:addOnClickEvent(npc.ui.makeSure_btn2, function()
GUI:setVisible(npc.ui.firstBg, false)
GUI:setVisible(npc.ui.secondBg, true)
GUI:setVisible(npc.ui.thirdBg, false)
end)
end
SL:RegisterLuaNetMsg(1017, networkCB)
```
主要修改点:
1. 提前创建输入框并注册事件
2. 使用`npc.inputValue`存储输入值,避免全局变量
3. 添加了输入值判空检查
4. 修正了Win_Create函数的参数
5. 添加了函数存在性检查
这样就不会出现"target is nil value"的错误了。
← 返回问答库