When I tried it in the current Godot 3.0 build, for whatever reason it seemed like Area2D's mouse_entered
and mouse_exited
events weren't firing properly. Maybe this is a long-standing bug even in 2.1.x?
When I changed the parent node to be a Control item like a PanelContainer, put the Area2D under that, and used the Control's mouse_entered
and mouse_exited
events, it worked fine.
Here is the code that I wrote. Assuming a .tscn with the root as a PanelContainer with name "PanelContainer" and a single child Area2D node with name "Area2D"
NOTE: You can also do this in JUST a Container-derived class, without the child class at all. I have written up an example here
# "PanelContainer"
extends PanelContainer
func _ready():
connect("mouse_entered", get_node("Area2D"), "on_mouse_entered")
connect("mouse_exited", get_node("Area2D"), "on_mouse_exited")
func on_mouse_hovering(p_mouse_position, p_mouse_speed):
print("hovering at position " + str(p_mouse_position) + " with velocity " + str(p_mouse_speed))
# Area2D
extends Area2D
signal mouse_hovering(position, velocity)
var is_hovering = false
export var hover_emission_rate = 1.0
var hover_accumulator = 0.0
func _ready():
set_fixed_process(true)
connect("mouse_hovering", get_parent(), "on_mouse_hovering")
func _fixed_process(delta):
hover_accumulator += delta
if is_hovering && hover_accumulator >= hover_emission_rate:
var true_speed = Vector2(0,0)
# if we changed position
if get_viewport().get_mouse_position() != last_mouse_position:
# then grab the latest mouse velocity
true_speed = Input.get_last_mouse_speed()
# Regardless, update the last known mouse position
last_mouse_position = get_viewport().get_mouse_position()
emit_signal("mouse_hovering", last_mouse_position, true_speed)
while hover_accumulator >= hover_emission_rate:
hover_accumulator -= hover_emission_rate
func on_mouse_entered():
is_hovering = true
func on_mouse_exited():
is_hovering = false