addMouseOverTips
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-04 22:48:59 |
👁️ 阅读: 2
`GUI:addMouseOverTips` 是用于为控件添加鼠标悬停提示的函数。根据浮生梦老师前端课程内容,该函数的使用方法如下:
```lua
-- 基本用法:添加固定文本提示
GUI:addMouseOverTips(widget, "提示文本", {x = 0, y = 0})
-- 动态提示:使用函数返回提示内容
GUI:addMouseOverTips(
widget,
function()
local level = SL:GetMetaValue("LEVEL")
return string.format("当前等级:%s", level)
end,
{x = 0, y = -20},
{x = 0.1, y = 0.5}
)
-- 带位置偏移的提示
GUI:addMouseOverTips(widget, "提示文本", {x = 20, y = 0}, {x = 0.5, y = 0.5})
```
**参数说明:**
- `widget`:控件对象,需要添加提示的UI控件
- 提示内容:可以是字符串,也可以是返回字符串的函数
- 位置偏移:`{x, y}` 表,控制提示框相对于鼠标位置的偏移
- 锚点偏移(可选):`{x, y}` 表,控制提示框内部的锚点位置
**使用示例:**
```lua
-- 为按钮添加简单提示
local button = GUI:Button_Create(parent, "btn_test", 100, 100, "res/public/1900000612.png")
GUI:addMouseOverTips(button, "点击我试试!", {x = 0, y = 10})
-- 为进度条添加动态提示
local loadingBar = GUI:LoadingBar_Create(parent, "LoadingBar_exp", 0, 0, 100, 20)
GUI:addMouseOverTips(
loadingBar,
function()
local curExp = SL:GetMetaValue("EXP")
local maxExp = math.max(SL:GetMetaValue("MAXEXP"), 1)
local per = math.min(curExp / maxExp * 100, 100)
return string.format("当前经验:%.2f%%", per)
end,
{x = 0, y = 0},
{x = 0.3, y = 0.5}
)
```
这个函数常用于为游戏中的各种UI控件添加说明性文字,提升用户体验。
← 返回问答库