Godot:检测 Area2D 内部的 "mouse down" 和 Area2D 外部的 "mouse up"
Godot: detect "mouse down" inside Area2D and "mouse up" OUTSIDE Area2D
我想检测 Area2D 内部的鼠标点击(并按住),然后检测 Area2D 内部或外部的鼠标释放。
这是我目前的情况:
extends Area2D #PickArea
func _input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if event.is_pressed():
print("picked")
else:
print("released") ## here be dragons. release only detected inside Area2D
上面的方法有效,但它只检测 Area2D 内的鼠标释放。如何在 Area2D 之外检测鼠标释放?
节点结构如下:
您可以在 _input
中获取 Area2D
之外的输入事件。使用它来获取版本:
func _input(event: InputEvent) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and not event.is_pressed()
):
print("released")
但是,您只有在得到媒体后才想得到它,对吗?因此,让我们在 _ready
和 _input
上禁用 _input
,并在 _input_event
上启用它:
extends Area2D
func _ready():
set_process_input(false)
func _input_event(viewport: Object, event: InputEvent, shape_idx: int) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and event.is_pressed()
):
print("picked")
set_process_input(true)
func _input(event: InputEvent) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and not event.is_pressed()
):
print("released")
set_process_input(false)
这应该可以解决问题。
我想检测 Area2D 内部的鼠标点击(并按住),然后检测 Area2D 内部或外部的鼠标释放。
这是我目前的情况:
extends Area2D #PickArea
func _input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if event.is_pressed():
print("picked")
else:
print("released") ## here be dragons. release only detected inside Area2D
上面的方法有效,但它只检测 Area2D 内的鼠标释放。如何在 Area2D 之外检测鼠标释放?
节点结构如下:
您可以在 _input
中获取 Area2D
之外的输入事件。使用它来获取版本:
func _input(event: InputEvent) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and not event.is_pressed()
):
print("released")
但是,您只有在得到媒体后才想得到它,对吗?因此,让我们在 _ready
和 _input
上禁用 _input
,并在 _input_event
上启用它:
extends Area2D
func _ready():
set_process_input(false)
func _input_event(viewport: Object, event: InputEvent, shape_idx: int) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and event.is_pressed()
):
print("picked")
set_process_input(true)
func _input(event: InputEvent) -> void:
if (
event is InputEventMouseButton
and event.button_index == BUTTON_LEFT
and not event.is_pressed()
):
print("released")
set_process_input(false)
这应该可以解决问题。