How can I use a function and play an animation at the same time

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By davidandersonsix
:warning: Old Version Published before Godot 3 was released.

In my project I have a character (rigidbody2d) who moves right with the function moveRight()
I want to play a walking animation while the action key is held (echo). When the key is tapped no animation is played but the character moves right.

Currently on echo the animation only plays the first frame until the key is released.On release the animation plays but at that point the sprite is no longer moving
What’s my issue here? I’d like the animation to play on a loop as long as the key is held.
Pertinent code below:

func moveRight():
	set_linear_velocity(Vector2(40, get_linear_velocity().y))
	pass

func _input(event):
	
	if event.is_action("ui_right"):
   		if not event.is_echo():
			moveRight()
		else:
			anim.play("walkRight")
			moveRight()
:bust_in_silhouette: Reply From: manokara

The problem is that walkRight is being executed from the beginning all the time. Change the anim.play to this:

if not anim.is_playing():
    anim.play("walkRight")

And it should work as expected.