local ui = {}
local _V = function(...) return SL:GetMetaValue(...) end
local FUNCQUEUE = {}
local TAGOBJ = {}
function ui.init(parent, __data__, __update__)
if __update__ then return ui.update(__data__) end
-- Create Node_1
local Node_1 = GUI:Nod
要在脚本中给Button_6添加关闭界面的功能,你需要先获取到Button_6的控件对象,然后给它添加点击事件。根据浮生梦老师前端课程内容,可以这样做:
```lua
-- 在ui.update函数中或者ui.init函数中添加按钮事件
function ui.update(data)
-- 先执行原有的队列函数
for _, func in pairs(FUNCQUEUE) do
if func then func(data) end
end
-- 获取Button_6控件
local Button_6 = GUI:getChildByName(Image_1, "Button_6")
if not GUI:Win_IsNull(Button_6) then
-- 添加关闭界面的点击事件
GUI:addOnClickEvent(Button_6, function()
-- 关闭当前界面
if parent and not GUI:Win_IsNull(parent) then
GUI:removeFromParent(parent)
SL:print("界面已关闭")
else
-- 如果无法获取父节点,尝试移除Node_1
local Node_1 = GUI:getChildByName(parent, "Node_1")
if not GUI:Win_IsNull(Node_1) then
GUI:removeFromParent(Node_1)
SL:print("界面已关闭")
end
end
end)
end
end
```
或者你也可以在ui.init函数中直接添加事件:
```lua
function ui.init(parent, __data__, __update__)
if __update__ then return ui.update(__data__) end
-- ... 原有的创建控件代码 ...
-- 在创建完Button_6后直接添加事件
-- Create Button_6
local Button_6 = GUI:Button_Create(Image_1, "Button_6", 688, 408, "res/custom/guanbi.png")
GUI:Button_setTitleText(Button_6, [[]])
GUI:Button_setTitleColor(Button_6, "#ffffff")
GUI:Button_setTitleFontSize(Button_6, 16)
GUI:Button_titleEnableOutline(Button_6, "#000000", 1)
GUI:setAnchorPoint(Button_6, 0.00, 0.00)
GUI:setTouchEnabled(Button_6, true)
GUI:setTag(Button_6, 0)
-- 添加关闭事件
GUI:addOnClickEvent(Button_6, function()
if parent and not GUI:Win_IsNull(parent) then
GUI:removeFromParent(parent)
SL:print("关闭按钮被点击,界面已关闭")
end
end)
ui.update(__data__)
return Node_1
end
```
**关键点说明:**
1. 使用`GUI:addOnClickEvent()`给按钮添加点击事件
2. 使用`GUI:removeFromParent()`移除父节点来关闭界面
3. 使用`GUI:Win_IsNull()`判断控件是否有效
4. 关闭后使用`SL:print()`输出提示信息
建议使用第二种方式,在创建按钮后立即添加事件,这样逻辑更清晰。记得在关闭前做好空值判断,避免程序崩溃。