Godot Version
4.2
Question
If you press the movement buttons during the character’s death animation, the animation begins to repeat
Code
extends CharacterBody2D
enum {
MOVE,
ATTACK1,
ATTACK2,
BLOCK,
SLIDE,
JUMP,
DEATH,
TAKE_DAMAGE
}
const SPEED = 150.0
const JUMP_VELOCITY = -320.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animation = $AnimatedSprite2D
@onready var animationPlayer = $AnimationPlayer
var health = 100
var gold = 0
var combo = false
var state = MOVE
func _physics_process(delta):
match state:
MOVE:
move_state()
ATTACK1:
attak_state()
ATTACK2:
attak2_state()
BLOCK:
block_state()
SLIDE:
slide_state()
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
animationPlayer.play("Jump")
if velocity.y>0:
animationPlayer.play("fall")
if health <= 0:
velocity.x = 0
health = 0
animation.play("death")
await animation.animation_finished
queue_free()
get_tree().change_scene_to_file.bind("res://death_screen.tscn").call_deferred()
move_and_slide()
func move_state ():
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if velocity.y == 0:
animationPlayer.play("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0:
animationPlayer.play("idle")
if direction == -1:
animation.flip_h = true
elif direction == 1:
animation.flip_h = false
if Input.is_action_just_pressed("block"):
if velocity.x == 0:
state = BLOCK
else:
state = SLIDE
if Input.is_action_just_pressed("attack"):
state = ATTACK1
func block_state():
velocity.x = 0
animationPlayer.play("block")
if Input.is_action_just_released("block"):
state = MOVE
func slide_state():
if is_on_floor():
animationPlayer.play("slide")
await animationPlayer.animation_finished
state = MOVE
func attak_state():
if Input.is_action_just_pressed("attack") and combo == true:
state = ATTACK2
velocity.x = 0
animationPlayer.play("attack1")
await animationPlayer.animation_finished
state = MOVE
func attak2_state():
animationPlayer.play("attack2")
await animationPlayer.animation_finished
state = MOVE
func combo1 ():
combo = true
await animationPlayer.animation_finished
combo = false`Preformatted text`