获取控件位置
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-06 00:19:32 |
👁️ 阅读: 7
要获取控件的位置,可以使用 `GUI:getPosition(widget)` 函数。它会返回一个包含 `x` 和 `y` 坐标的 table。
**示例代码:**
```lua
-- 假设你已经有一个控件对象,比如一个按钮
local myButton = GUI:Button_Create(parent, "btn_test", 100, 100, "res/public/1900000612.png")
-- 获取该按钮的位置
local pos = GUI:getPosition(myButton)
-- 打印坐标
SL:print("控件X坐标: " .. pos.x)
SL:print("控件Y坐标: " .. pos.y)
```
**说明:**
- `GUI:getPosition(widget)` 的参数 `widget` 是你要查询的控件对象。
- 返回值 `pos` 是一个 table,你可以通过 `pos.x` 和 `pos.y` 来分别访问横坐标和纵坐标。
如果你想单独获取控件的世界坐标(相对于整个屏幕的坐标),可以使用 `GUI:getWorldPosition(widget)`,用法类似。
← 返回问答库