I want to create a timer when my player dies after 3 seconds changes the scene to the menu. My player code
extends KinematicBody2D
var speed = 500 var acceleration = 800 var deceleration = 1000 var velocity = Vector2.ZERO var axis = Vector2.ZERO var rotation_speed = 500 var target_rotation = 0.0 var current_rotation = 0.0 var return_speed = 5 var MAX_ROTATION_DEGREE = 30 var PopEffect = preload(“res://Scenes/pop.tscn”)func _ready(): self.connect(“area_entered”, self, “_on_Area2D_area_entered”) func _physics_process(_delta):axis.x = Input.get_action_strength(“right”)-Input.get_action_strength(“left”)
axis.y = Input.get_action_strength(“down”)-Input.get_action_strength(“up”)
I am getting an error the method change the scene isnt declared …
so i just change it with this
func die():
print(“you are dead”)
var pop_effect_instance = PopEffect.instance()
pop_effect_instance.position = position
pop_effect_instance.emitting = true
get_parent().add_child(pop_effect_instance)
queue_free()
yield(get_tree().create_timer(3), “timeout”) get_tree().change_scene(“res://Scenes/Menu.tscn”)
but it dont change the scene so i dont understand why
thats because you use queue_free() → your object deletes itself before the timer runs out. A fix would be to call a method on the parent of the player:
func die():
print(“you are dead”)
var pop_effect_instance = PopEffect.instance()
pop_effect_instance.position = position
pop_effect_instance.emitting = true
get_parent().playerDied(pop_effect_instance)
queue_free()