获取控件位置
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-27 22:25:14 |
👁️ 阅读: 16
根据浮生梦老师前端课程内容,获取控件位置需要使用 `GUI:getPosition(widget)` 函数。
这个函数会返回一个包含 x 和 y 坐标的 table 数据。
**示例代码:**
```lua
-- 假设你已经有一个控件对象 myWidget
local pos = GUI:getPosition(myWidget)
if pos then
SL:Print("控件X坐标:", pos.x)
SL:Print("控件Y坐标:", pos.y)
end
```
**注意事项:**
- `getPosition` 获取的是控件相对于其父节点的本地坐标
- 如果需要获取控件在屏幕上的世界坐标,可以使用 `GUI:getWorldPosition(widget)` 函数
- 在获取坐标前最好先判断控件对象是否存在,避免空值错误
← 返回问答库