Hey there! I'm currently working on making a game and I just implemented a lives program. When the player gets hit by an enemy, it losses a heart and respawns at the start of the level. However, after respawning, if the player gets hit again, it'll be trapped in the dead animation forever. How can I fix it? This is my first time doing this, so anything helps! Thanks:)
Code for Player:
extends KinematicBody2D
onready var animated_sprite : AnimatedSprite = $AnimatedSprite
export var move_speed : = 250.0
export var push_speed : = 125.0
const Enemy: = preload("res://assets/sprites/world/Enemy.gd")
var is_dead = false
var velocity = Vector2()
var respawnpoint : Vector2
func _ready():
respawnpoint = global_position
func _physics_process(_delta: float) -> void:
if is_dead == false:
var motion : = Vector2()
motion.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
motion.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
update_animation(motion)
move_and_slide(motion.normalized() * move_speed, Vector2())
if get_slide_count() > 0:
check_box_collision(motion)
func update_animation(motion: Vector2) -> void:
var animation = "idle"
if motion.x > 0:
animation = "right"
elif motion.x < 0:
animation = "left"
elif motion.y < 0:
animation = "up"
elif motion.y > 0:
animation = "down"
if animated_sprite.animation != animation:
animated_sprite.play(animation)
func check_box_collision(motion : Vector2) -> void:
if abs(motion.x) + abs(motion.y) > 1:
return
var box: = get_slide_collision(0).collider as Box
if box:
box.push(push_speed * motion)
func dead():
is_dead = true
velocity = Vector2(0,0)
$AnimatedSprite.play("dead")
$CollisionShape2D.call_deferred("set_disabled",true)
$Timer.start()
func _on_Timer_timeout():
is_dead = false
global_position = respawnpoint
velocity = Vector2()
$CollisionShape2D.call_deferred("set_disabled",false)
func _on_Area2D_body_entered(body):
if body is Enemy:
dead()
Code for Enemy:
extends KinematicBody2D
const SPEED = 100
const GRAVITY = 10
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var direction = 1
func _physics_process(delta):
velocity.x = SPEED * direction
if direction == 1:
$AnimatedSprite.flip_h = false
else:
$AnimatedSprite.flip_h = true
$AnimatedSprite.play("walk")
velocity.y += GRAVITY
velocity = move_and_slide(velocity, FLOOR)
if is_on_wall():
direction = direction * -1
if get_slide_count() > 0:
for i in range (get_slide_count()):
if "Player" in get_slide_collision(i).collider.name:
get_slide_collision(i).collider.dead()
Code for Heartbar:
extends Control
var current_health = 3 setget update_bars
const Enemy: = preload("res://assets/sprites/world/Enemy.gd")
func _ready():
self.current_health = 3
func update_bars(value):
current_health = value
# print("Health: ", current_health)
for bar in $Bars.get_children():
bar.update_health(current_health)
func _on_Area2D_body_entered(body):
if body is Enemy:
self.current_health -= 1
if self.current_health == 0:
get_tree().change_scene("res://assets/sprites/world/TitleScreen.tscn")