I want when my player dies the main timer stops counting time and then after it show my record time.
My Timer
extends Label
var time_alive: float = 0.0
func _ready():
# Set the initial text of the Label
text = str(int(time_alive))
# Start processing every frame
set_process(true)
func _process(delta):
# Increment the time_alive by the time passed since the last frame
time_alive += delta
# Update the Label's text to show the elapsed time in seconds
text = str(int(time_alive))
My player
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")
axis = axis.normalized() * speed
velocity = velocity.linear_interpolate(axis, 0.025)
velocity = move_and_slide(velocity)
func _process(delta):
if Input.is_action_pressed("right"):
target_rotation = deg2rad(MAX_ROTATION_DEGREE)
elif Input.is_action_pressed("left"):
target_rotation = deg2rad(-MAX_ROTATION_DEGREE)
else:
target_rotation = 0.0
current_rotation = lerp_angle(current_rotation, target_rotation, return_speed * delta)
rotation = current_rotation
rotation = clamp(rotation, -PI/8, PI/8)
func _on_Area2D_area_entered(area):
if area.is_in_group("Bullet"):
die()
print("bullet")
if area.is_in_group("Enemy"):
die()
print("enemy")
if area.is_in_group("Border"):
die()
print("border")
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()
Okay, i will add my code as you said, with this timer there nothing wrong, i just want to stop my timer when player dies and then create an high score label that will say my maximum time.I try do this but it didnt work.
extends Label
var time_alive: float = 0.0
var running: bool = true
func _ready():
# Set the initial text of the Label
text = str(int(time_alive))
# Start processing every frame
set_process(true)
func _process(delta):
if running:
# Increment the time_alive by the time passed since the last frame
time_alive += delta
# Update the Label's text to show the elapsed time in seconds
text = str(int(time_alive))
func stop_timer():
running = false
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")
onready var timer_label = $Timer1
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")
axis = axis.normalized() * speed
velocity = velocity.linear_interpolate(axis, 0.025)
velocity = move_and_slide(velocity)
func _process(delta):
if Input.is_action_pressed("right"):
target_rotation = deg2rad(MAX_ROTATION_DEGREE)
elif Input.is_action_pressed("left"):
target_rotation = deg2rad(-MAX_ROTATION_DEGREE)
else:
target_rotation = 0.0
current_rotation = lerp_angle(current_rotation, target_rotation, return_speed * delta)
rotation = current_rotation
rotation = clamp(rotation, -PI/8, PI/8)
func _on_Area2D_area_entered(area):
if area.is_in_group("Bullet"):
die()
print("bullet")
if area.is_in_group("Enemy"):
die()
print("enemy")
if area.is_in_group("Border"):
die()
print("border")
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)
timer_label.stop_timer()
queue_free()
Your player doesn’t have a child node $Timer1 that’s a child of the Game node. You could use export or move the label under the player; probably best with a canvas layer