ClickDetector
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
ClickDetector は、Scripts および LocalScripts
デフォルトのコントロールスクリプトは、ButtonR2 を介して、ClickDetectors を使用し
MaxActivationDistance は、プレイヤーがクリック可能な状態ではなくなる前にクリックディテクターからの距離を制限するために使用できます。
ClickDetector イベントは
優先度を入力
複数の ClickDetectors がユーザーの入力を検出する場合、最も深いものがイベントを発動させます。2つの ClickDetectors が兄弟である場合、最も古いものが優先されます。
Class. ContextActionService にバインドされたアクションは、ClickDetector と同じ入力を使用すると、ContextActionService のアクションがクリックディテクターのイベントの優先度を上回ります。
UserInputService.InputBegan は、ClickDetector イベントの前に発動します。
コードサンプル
Place this code inside a Script inside a ClickDetector. The code sample creates a reference to the parent and defines a function to show a message that greets a player. Finally, it connects the MouseClick event to the defined function.
local clickDetector = script.Parent
local function onClicked(player)
-- Show a message to the player
local msg = Instance.new("Message")
msg.Parent = player:FindFirstChild("PlayerGui")
msg.Text = "Hello, " .. player.Name
wait(2.5)
msg:Destroy()
end
-- Connect the function to the MouseClick event
clickDetector.MouseClick:Connect(onClicked)
This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).
local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
概要
プロパティ
マウスがこの ClickDetector または DragDetector の上にあるときにカーソルアイコンを表示します。
プレイヤーがインタラクトできるようにするための最大距離は、ClickDetector または DragDetector です。
イベント
プレイヤーが ClickDetector または DragDetector の親とインタラクトしたときにファイアを起動します。
Class.ClickDetector または DragDetector の親がプレイヤーの上にマウスポインタを置くときに発動します。
プレイヤーのカーソルが親の ClickDetector または DragDetector の上に停止すると、ファイアーを起動します。
プレイヤーが ClickDetector または DragDetector のマウスカーソルを右クリックしたときに発動します。
プロパティ
CursorIcon
マウスがこの ClickDetector または DragDetector の上にあるときにカーソルアイコンを表示します。このプロパティを空の場合は、ディテクターはデフォルトのアイコンを使用します。
カーソルアイコンを変更するには、このプロパティを使用したい画像のアセット ID に設定します。
MaxActivationDistance
このプロパティは、プレイヤーがインタラクトできるようにするために、Character とClickDetector またはDragDetector の最大距離を、ス
方法
イベント
MouseClick
このイベントは、Script または LocalScript から発生し、次の入力経由で ClickDetector または 1>Class.DragDetector1> を通じてプレイヤーと対話するときに発生します:
- マウスのあるプラットフォームで、プレイヤーがマウスを離れたときに。
- オン TouchEnabled プラットフォームで、プレイヤーがタップするとき。
- オン GamepadEnabled プラットフォームで、中央ドットが同じモデルにあり、 A ボタンを押してリリースするときに。
注意: プレイヤーの Character は、MaxActivationDistance のディテクターの Class.ClickDetector.MaxActivationDistance|Class.Player.Character 内にある必要があります。
パラメータ
親の Player または ClickDetector をクリックした DragDetector。
MouseHoverEnter
このイベントは、Script または LocalScript の親が、ClickDetector または 1>Class.DragDetector1> の上にマウスポイントを置くと
ユーザーの入力の性質により、MouseHoverEnter のすべてのイベントが、対応する MouseHoverLeave イベントを発動させる必要はありません。
パラメータ
Class.Player は、ClickDetector または DragDetector の親をホバーオーバーし始めます。
コードサンプル
The following code will print "[PlayerName] hovered over my parent!" when a player's cursor hovers over the ClickDetector parent. It will also print "[PlayerName] hovered off my parent!" when the player's cursor moves off the ClickDetector parent.
In order for this example to work as expected, it must be placed in a Script or LocalScript whose parent is a ClickDetector.
local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)
MouseHoverLeave
このイベントは、Script または LocalScript のいずれかがプレイヤーのカーソルを親に置くときに発生します。これは、ClickDetector また
ユーザーの入力の性質により、MouseHoverLeave イベントが発動する後、MouseHoverEnter すべてのイベントに依存する必要はありません。
パラメータ
Class.Player のカーソルが親の ClickDetector または DragDetector の上にある場合。
コードサンプル
The following code will print "[PlayerName] hovered over my parent!" when a player's cursor hovers over the ClickDetector parent. It will also print "[PlayerName] hovered off my parent!" when the player's cursor moves off the ClickDetector parent.
In order for this example to work as expected, it must be placed in a Script or LocalScript whose parent is a ClickDetector.
local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)
RightMouseClick
このイベントは、Script または LocalScript から発生します。ClickDetector または 1>Class.DragDetector1> のマウスカーソルを右クリックしたとき、プレイヤ
パラメータ
Class.Player は、ClickDetector または DragDetector の親にマウスカーソルを右クリックします。