0 votes

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.
in Engine by (69 points)

1 Answer

0 votes

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...

by (27,902 points)

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 onattackbodyshapeentered then when i pres the attack botton it call the signal _onplayerhit then it reduces health until it hit 0 then an if methud call queuefree() .
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

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.

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

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.

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

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.