mouse - Godot:检测 Area2D 内部的 "mouse down"和 Area2D 外部的 "mouse up"

标签 mouse godot gdscript

我想检测 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 之外检测鼠标释放?

这是节点结构:

enter image description here

最佳答案

您可以在 _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)

这应该可以解决问题。

关于mouse - Godot:检测 Area2D 内部的 "mouse down"和 Area2D 外部的 "mouse up",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69498580/

相关文章:

javascript - Tinymce 双击激活

c# - 将键盘事件发送到 C# 中不处理 Windows 事件的另一个应用程序

javascript - 如何使用 jquery 创建 move 句柄

godot - 输入 "Any"的正确方法

2d - Godot 使元素跟随鼠标

c# - Windows 中的自动完成文本框和 "Hide Pointer While Typing"

godot - 如何从 Godot 中的字符串中删除所有标点符号?

godot - 在 Area2D 中覆盖 KinematicBody2D 运动?

godot - 如何在 Godot 中创建可扩展的多行按钮?

python - 为什么将一个变量设置为等于另一个变量会改变此代码中的任何内容?