我正在尝试获取一个动态实例化的kinematicBody2D,该实例带有附加的2D区域以处理鼠标的输入/退出输入。我创建了具有正确碰撞体的2D区域,并测试了类似的碰撞体以检测某些2d区域,并且此操作很愉快,但是,鼠标检测并未触发应有的功能。
我不确定为什么它似乎没有检测到我的鼠标。我以为我误用了Masks,它不在同一个级别上,但是查看一些文档,这不建议是一个问题。
我不确定要附加什么代码,因为此时尚未真正编码。
任何帮助将不胜感激。
答案 0 :(得分:0)
要检测区域或KinematicBody上的鼠标事件,请将input_pickable设置为true
,然后连接到provided signals的一个或多个。
KinematicBody2D
和Area2D
都继承自CollisionObject2D
,因此它们都可以处理鼠标输入。这意味着您不需要需要在您的Area
中添加KinematicBody
,除非检测点击的区域必须不同于该区域检测碰撞(例如,只有较大对象的一小部分是可单击的)。
以下是通过KinematicBody
来检测CollisionShape
上的鼠标事件的方法:
func _ready():
input_pickable = true
connect("mouse_entered", self, "_on_mouse_entered")
connect("mouse_entered", self, "_on_mouse_entered")
connect("input_event", self, "_on_input_event")
func _on_mouse_entered():
print("mouse entered")
func _on_mouse_exited():
print("mouse exited")
func _on_input_event(viewport, input_event, shape_idx):
var mouse_event = input_event as InputEventMouseButton
if mouse_event:
prints("Mouse button clicked:", mouse_event.button_index)