Hi there! I'm trying to code my first game and am using a bunch of tutorials to piece them together. I'm trying to have it to when an enemy hits the player, it restarts the level (maybe lives in the future? Not sure how to do that). Currently, when my player is standing still and gets hit, it will play the animation and reload the current scene. But when the player is moving, it just gets trapped in the animation. Also, how can I make it to where everything on the screen freezes while the player is going through the dead animation?
Code for player:
extends KinematicBody2D
onready var animated_sprite : AnimatedSprite = $AnimatedSprite
export var move_speed : = 250.0
export var push_speed : = 125.0
var is_dead = false
var velocity = Vector2()
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)
if get_slide_count() > 0:
for i in range(get_slide_count()):
if "Enemy" in get_slide_collision(i).collider.name:
dead()
func dead():
is_dead = true
velocity = Vector2(0,0)
$AnimatedSprite.play("dead")
$CollisionShape2D.call_deferred("set_disabled",true)
$Timer.start()
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 _on_Timer_timeout():
get_tree().reload_current_scene()
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()
Thanks in advance:)