lua - 我正在尝试编写一个脚本来告诉我鼠标何时进入和离开 Roblox Studio 中的 ClickDetector
问题描述
这是代码。
local detector = script.Parent.ClickDetector -- setting a click detector variable to save time
function mousein() -- mousein function
print("Mouse has entered!") -- print "Mouse has entered!" in the output
end -- end of the function
function mouseout()-- mouseout function
print("Mouse has left!") -- print "Mouse has left!" in the output
end -- end of the function
detector.MouseHoverEnter:Connect(mousein) -- run mousein when my mouse hovers over the clickdetector
detector.MouseHoverLeave:Connect(mouseout)-- run mouseout when my cursor leaves the clickdetector
我标记了代码以使其更易于阅读。
请将工作代码放在答案中!
这是我在 StackOverflow 上的第一个问题。我是编码新手。
解决方案
您需要创建ClickDetector
实例。
local detector = Instance.new("ClickDetector")
detector.Parent = thing --whatever object you want to detect mouse stuff on
detector.MaxActivationDistance = 10
function mousein()
print("Mouse has entered!")
end
function mouseout()
print("Mouse has left!")
end
detector.MouseHoverEnter:Connect(mousein)
detector.MouseHoverLeave:Connect(mouseout)
或者,如 API 文档中所述,为了script.Parent.ClickDetector
按预期工作,它必须放在Script
LocalScript
ClickDetector
推荐阅读
- php - 未加载资产
- pandas - 如何最小化遍历完整图的路径并找到最佳起点?
- c# - 使用 httpClient.PostAsync Result = "{Not yet computed}" 时出现问题
- laravel - Laravel SPA中最干净的请求方式是什么
- linux - 从 linux 中的屏幕或脚本命令登录时如何忽略控制键输出?
- c++ - 如何获取用户输入来命名文件
- python - 如何修改 DataFrame 以使它们在 Pandas 中只有具有共享索引值的行?
- ios - “无法推断复杂的闭包返回类型;添加显式类型以消除歧义”SwiftUI 中的错误
- firebase - FCM 云功能打字稿到设备
- function - 比较函数作为球拍中的参数