How to make my attack animation possible with only one tap, instead of holding it down?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mcdodger

Hi, I am currently having trouble where I am trying to play the attack animation like any normal game where you just press the button and it plays the animation. At first, the animation wasn’t playing at all, I fixed that, but then ran into another problem where it would play the animation continously while the character is moving either left or right ( I couldn’t change directions).

Now I got it to move the character left and right, but the animation is still continuously playing.

Any Help?

Video Display of Problem

extends KinematicBody2D
# class member variables go here, for example:
# var a = 2
# var b = "textvar"

const SPEED = 60
const GRAVITY = 10
const JUMP_POWER = -260 # ' - ' in the y-axis means up
const FLOOR = Vector2(0,-1)

var velocity = Vector2()
var on_ground = false
var attack = false
var animation = ""

#to use an object use $NameofObject

func _input(event):
	#attack = false
	if Input.is_key_pressed(KEY_X) and is_on_floor():
		attack = true

func _physics_process(delta):
	print("attack: ",attack)
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
		animation = "run"
		$AnimatedSprite.flip_h = false
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED
		animation = "run"
		$AnimatedSprite.flip_h = true
		velocity.x = 0
		if on_ground == true:
			animation = "idle"

	if Input.is_action_pressed("ui_up"):
		if on_ground == true:
			velocity.y = JUMP_POWER
			on_ground = false
	velocity.y += GRAVITY
	if is_on_floor(): #not in the air
		on_ground = true
	else: # in the air
		on_ground = false
		if velocity.y < 0:
			animation = "jump"
		elif (velocity.y >= 0) and velocity.y != 20:
			animation = "fall"
	if attack:
		animation = ("ground-attack1")
	#attack = false
	velocity = move_and_slide(velocity, FLOOR)
:bust_in_silhouette: Reply From: TheBiggestNoob

Hey I have the same problem almost, but mine is worst. Instead my sprite freezes whenever I press the key that is supposed to make the sprite attack.

if Input.is_key_pressed(KEY_E):
print(“Attack is working”)

this is my code. Is there anything wrong with it. Sorry if you expected an answer, I am like, a really big noob.