Why isn't my sprite animation playing upon collision?

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

I want to trigger an attack animation once both the sprites collide with each other.

enemy.gd:

if get_slide_count()>0:
	for x in range(get_slide_count()):
		if "enemy_2" in get_slide_collision(x).collider.name:
			 $Sprite.play("Attack")

enemy_2.gd:

if get_slide_count()>0:
	for x in range(get_slide_count()):
		if "enemy" in get_slide_collision(x).collider.name:
			 $sprite.play("Attack")

But when I run the game, the sprites move away from each other towards opposite directons, upon colliding. Why is this happening?