enemy killing issue

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

my issue is when i try to kiil one enemy all enemys die ; player code:

extends KinematicBody2D
var motion = Vector2()
const UP = Vector2(0,-1)
const GRAVITY = 20
const SPEED = 200
const JAMP_HEIGHT = -600
signal hit
var derictions=1
signal deriction
var attack_side=false
var lefted_jamps = 2

func _process(delta):

if $Sprite.flip_h==false:
	derictions=1
else:
	derictions=-1
motion.y += GRAVITY
if Input.is_action_pressed("ui_right"):
	$Sprite.flip_h = false
	$Sprite.play("run")
	motion.x = SPEED
	
elif Input.is_action_pressed("ui_left"):
	$Sprite.flip_h = true
	$Sprite.play("run")
	motion.x = -SPEED
	
	
else :
	
	$Sprite.play("idle")
	motion.x = 0
motion = move_and_slide(motion,UP)
if is_on_floor():
	lefted_jamps=2
	
else :
	$Sprite.play("jump")
if Input.is_action_pressed("ui_attack"):
	$Sprite.play("atack")
	emit_signal("hit",derictions)
	
if lefted_jamps>0:
	if Input.is_action_just_pressed("ui_up"):
		lefted_jamps-=1
		motion.y=JAMP_HEIGHT
		

enemy code :

     extends KinematicBody2D
    const GRAVITY = 20
    const SPEED = 100
    const UP = Vector2(0,-1)
    var velocity = Vector2()
    var direction = 1
    var health = 3
    var enter_attack=false
    var enter_entred = false
   var hit_speed = 1000
  func _ready():
pass # Replace with function body.

  func _process(delta):
velocity.x = SPEED * direction
velocity.y += GRAVITY
$AnimatedSprite.play("walk")
velocity = move_and_slide(velocity,UP)
if enter_entred == false:
	
	if is_on_wall():
		direction = direction * -1
	
if health==0:
	queue_free()

if direction == 1 :
	$AnimatedSprite.flip_h = false
else :
	$AnimatedSprite.flip_h = true



    func _on_player_hit(derictions):
if enter_attack==true:
	health-=1
	velocity.x = hit_speed*derictions
	
	
pass # Replace with function body.










       func _on_attack_body_shape_entered(body_id, enemy, body_shape, area_shape):
   enter_attack=true


pass # Replace with function body.








          func _on_entred_body_shape_entered(body_id, enemy, body_shape, area_shape):
      enter_entred=true
pass # Replace with function body.


      func _on_entred_body_shape_exited(body_id, enemy, body_shape, area_shape):

enter_entred=false
pass # Replace with function body.


     func _on_attack_body_shape_exited(body_id, body, body_shape, area_shape):
enter_attack=false
pass # Replace with function body.
:bust_in_silhouette: Reply From: Zylann

So from what I read, your enemies die because health is 0.
The only code that reduces health is the following one, which I guess you connected to ALL enemies:

func _on_player_hit(derictions):
	if enter_attack==true:
		health-=1
		velocity.x = hit_speed*derictions

So it means enter_attack is true. You should check what sets this to true, maybe try to print or put a breakpoint in here and see if all enemies get it:

func _on_attack_body_shape_entered(body_id, enemy, body_shape, area_shape):
	enter_attack=true

Maybe your attack shape is too big?

Also, please check indentation and whitespace of your code, it’s makes it harder to read…

when enter attack is true is when they enter an erea2d called attack ;and it is so close from the player it is as far as its weapon .
what hapend is wen i attack a random enemey it get into attack area2d and it call signal _on_attack_body_shape_entered then when i pres the attack botton it call the signal _on_player_hit then it reduces health until it hit 0 then an if methud call queue_free() .
i think if i hit a player the health reduces on all enemy code and thats why they all die .
what i want from you is how can i attack spesifec enemy and only he die

abdolilah | 2019-07-25 14:46

the project :
https://drive.google.com/open?id=1Og_M7c6-DKPfKJ36awsa_6ohmD4_6Xr4

abdolilah | 2019-07-25 14:51

Did you actually check if _on_attack_body_shape_entered and _on_attack_body_shape_exited are called the way you expect them to be? Take a moment to inspect what happens using the debugger or adding print statements.

Zylann | 2019-07-26 13:12

i did visible collision shapes in the debug
ther is nothing wrong you can check the project : https://drive.google.com/open?id=1Og_M7c6-DKPfKJ36awsa_6ohmD4_6Xr4

abdolilah | 2019-07-26 13:41

I checked your project and it works as expected.
I got close to an enemy, pressed K 30 times because it has 30 health, then the enemy died, and only that enemy. The other one was still alive.

Zylann | 2019-07-26 19:02

i am sorry i made same changes to the way to kill enemys

abdolilah | 2019-07-26 19:41

when you kill an enemy try to kill the other one and you will notis that he dies with one attack

abdolilah | 2019-07-26 22:58

did you see -------------------

abdolilah | 2019-07-27 17:43