if the player is standing, the animation of standing still is played, if the player is running, the animation of running is played, if the player is hitting while standing, the animation of hitting is not played, but the animation of standing is played, as with the animation of running.
here is my code:
extends CharacterBody2D
@onready var animated_srtite_2d = $AnimatedSprite2D
var max_speed = 70
var current_dir = “none”
var enemy_inattack_range = false
var enemy_attack_cooldown = true
var health = 200
var player_alive = true
var attack_ip = false
Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
@onready var anim = $AnimatedSprite2D
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
enemy_attack()
attack()
if health <= 0:
player_alive = false # go back to respaun
health = 0
print(“Player defeat”)
self.queue_free()
var direction = movement_vector().normalized()
velocity = max_speed * direction
move_and_slide()
if direction.x != 0 || direction.y != 0:
if attack_ip == false:
animated_srtite_2d.play(“run”)
else:
if attack_ip == false:
animated_srtite_2d.play(“”)
var face_sign = sign(direction.x)
if face_sign !=0:
animated_srtite_2d.scale.x = face_sign
func movement_vector():
var movement_x = Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”)
var movement_y = Input.get_action_strength(“move_down”) - Input.get_action_strength(“move_up”)
return Vector2(movement_x,movement_y)
func player():
pass
func _on_player_hitbox_body_entered(body):
if body.has_method(“enemy”):
enemy_inattack_range = true
func _on_player_hitbox_body_exited(body):
if body.has_method(“enemy”):
enemy_inattack_range = false
func enemy_attack():
if enemy_inattack_range and enemy_attack_cooldown == true :
health = health -20
enemy_attack_cooldown = false
$Cooldown.start()
print(health)
if attack_ip == true :
$AnimatedSprite2D.play("hit")
func _on_cooldown_timeout():
enemy_attack_cooldown = true
func attack():
var dir = current_dir
if Input.is_action_just_pressed("hit"):
global.player_current_attack = true
attack_ip = true
if dir == "right":
$AnimatedSprite2D.flip_h = false
await($AnimatedSprite2D)
$AnimatedSprite2D.play("hit")
$deal_attack_timer.start()
if dir == "left":
$AnimatedSprite2D.flip_h = true
await($AnimatedSprite2D)
$AnimatedSprite2D.play("hit")
$deal_attack_timer.start()
else:
global.player_current_attack = false
attack_ip = false
func _on_deal_attack_timer_timeout():
$deal_attack_timer.stop()
global.player_current_attack = false
attack_ip = false