A文件:
GUI:Timeline_StopAll(MainMiniMap._ui.Node)
local clipWidth = GUI:getContentSize(MainMiniMap._ui["LayoutClip"]).width
if MainMiniMap._showState then
local mapBGWidth = GUI:getContentSize(MainMiniMap._ui["MapBG"]).width
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-20 19:35:50 |
👁️ 阅读: 18
在A文件中调用B文件的控件,可以通过以下两种方式实现:
## 方法一:全局变量传递(推荐)
在B文件中将控件设为全局变量或模块变量,在A文件中直接调用:
```lua
-- B文件
buttonLayout = GUI:getChildByName(parent, "buttonLayout")
topIcons = GUI:getChildByName(parent, "topIcons")
Button = GUI:getChildByName(parent, "Button")
-- A文件
if buttonLayout and not GUI:Win_IsNull(buttonLayout) then
GUI:Timeline_MoveTo(buttonLayout, { x = -475, y = -265 }, 0.2)
end
```
## 方法二:通过函数参数传递
在B文件中创建函数返回控件,A文件调用该函数:
```lua
-- B文件
local function getMiniMapControls()
return {
buttonLayout = GUI:getChildByName(parent, "buttonLayout"),
topIcons = GUI:getChildByName(parent, "topIcons"),
Button = GUI:getChildByName(parent, "Button")
}
end
-- A文件
local controls = getMiniMapControls()
if controls.buttonLayout then
GUI:Timeline_MoveTo(controls.buttonLayout, { x = -475, y = -265 }, 0.2)
end
```
## 注意事项:
1. 使用前必须判断控件是否存在:`if widget and not GUI:Win_IsNull(widget) then`
2. 确保B文件在A文件之前执行,控件已经创建完成
3. 如果控件在界面关闭时被销毁,需要重新获取
这样A文件就能安全地操作B文件创建的控件了。记得检查空值,避免运行时错误!
在A文件中调用B文件的控件,可以通过以下两种方式实现:
## 方法一:全局变量传递(推荐)
在B文件中将控件设为全局变量或模块变量,在A文件中直接调用:
```lua
-- B文件
buttonLayout = GUI:getChildByName(parent, "buttonLayout")
topIcons = GUI:getChildByName(parent, "topIcons")
Button = GUI:getChildByName(parent, "Button")
-- A文件
if buttonLayout and not GUI:Win_IsNull(buttonLayout) then
GUI:Timeline_MoveTo(buttonLayout, { x = -475, y = -265 }, 0.2)
end
```
## 方法二:通过函数参数传递
在B文件中创建函数返回控件,A文件调用该函数:
```lua
-- B文件
local function getMiniMapControls()
return {
buttonLayout = GUI:getChildByName(parent, "buttonLayout"),
topIcons = GUI:getChildByName(parent, "topIcons"),
Button = GUI:getChildByName(parent, "Button")
}
end
-- A文件
local controls = getMiniMapControls()
if controls.buttonLayout then
GUI:Timeline_MoveTo(controls.buttonLayout, { x = -475, y = -265 }, 0.2)
end
```
## 注意事项:
1. 使用前必须判断控件是否存在:`if widget and not GUI:Win_IsNull(widget) then`
2. 确保B文件在A文件之前执行,控件已经创建完成
3. 如果控件在界面关闭时被销毁,需要重新获取
这样A文件就能安全地操作B文件创建的控件了。记得检查空值,避免运行时错误!
← 返回问答库