how to make Area2D recognize enemy

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bárbara Gabriela

I put an Area2D into the player called “hit_area”, inside it a CollisionShape2D, then plugged a function into the player script so that when the enemy touches the player it would appear another KinematicBody2D in this scene that follows the player, but as soon as I start the game the 2 KinematicBody2D appears without even touching the enemy. How can I fix it?

Here’s the code:

extends KinematicBody2D

var state = 1
const slimezinho = preload("res://scenes/slimes/slimezinho.tscn")
const mini_slimezinhoinho = preload("res://scenes/mini_slimezinhoinho.tscn")

func _ready():
    pass

func _process(delta):
    #multiply
    if(state == 2):
	    var slime = slimezinho.instance()
	    get_parent().add_child(slime)
	    slime.set_position(get_node("spawn").get_global_position())
	    pass
    if(state == 3):
	    var slime = mini_slimezinhoinho.instance()
	    get_parent().add_child(slime)
	    slime.set_position(get_node("spawn").get_global_position())
	    pass
    pass

#change state
func _on_hit_area_body_entered(enemy):
    state += 1
    pass
:bust_in_silhouette: Reply From: Magso

The body_entered signal has the body parameter to reference the intersecting body. Either use a group or the node name to check when body is the enemy.

func _on_hit_area_body_entered(body):
    if body.is_in_group("Enemies"):
        state += 1