Godot 4.4
I am trying to change a scene, with a fade-to-black transition, when an Area2D is clicked on. However, the mouse will automatically click twice, leading to the scene not loading. This problem is solved when I take out the “await” line. I have been able to use the “await” line previously in a similar context.
Here is the code I am trying to use. This is in a general main script:
extends Node2D
func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
print("Inspecting the hole in the wall")
TransitionScene.transition()
await TransitionScene.on_transition_finished
get_tree().change_scene_to_file("res://scenes/hole.tscn")
Here is the code in a similar context that works. This code is attached to the object being clicked:
extends Area2D
func _input_event(viewport, event, shape_idx):
if event.is_action_pressed("click"):
print("Inspecting The Tower")
TransitionScene.transition()
await TransitionScene.on_transition_finished
get_tree().change_scene_to_file("res://scenes/tower_inspection.tscn")
Here is the code for the transition scene:
extends CanvasLayer
signal on_transition_finished
@onready var color_rect: ColorRect = $ColorRect
@onready var animation_player: AnimationPlayer = $AnimationPlayer
func _ready():
color_rect.visible = false
animation_player.animation_finished.connect(_on_animation_finished)
func _on_animation_finished(anim_name):
if anim_name == "fade_to_black":
on_transition_finished.emit()
animation_player.play("fade_to_normal")
elif anim_name == "fade_to_normal":
color_rect.visible = false
func transition():
color_rect.visible = true
animation_player.play("fade_to_black")