Enemie error death and hitflash

Godot Version

4.1.2
I am having two errors with the enemy that I do not understand, first that when the enemy reaches the player he dies but only if I shoot him instead of when I shoot him and his life runs out he dies without having to reach me and then the enemy starts again white color of the hit flash animation although I didn’t put any of that in the code
Sorry if some things are in Spanish, vida is life.


@export var movement_speed = 55

@onready var player =  get_tree().get_first_node_in_group("player")
@onready var sprite = $Sprite2D
@onready var vida = 1
@onready var flash = $hitflash



func _physics_process(_delta):
	if is_instance_valid(player):
		var direction = global_position.direction_to(player.global_position)
		velocity = direction*movement_speed
		move_and_slide()
		

		if direction.x > 0:
			sprite.flip_h = false
		
		elif direction.x < 0:
			sprite.flip_h = true
		
		

func _ready():
	$anim.play("idle")


func _on_area_2d_area_entered(_area):
	if _area.is_in_group("bala"):
		flash.play("flash")
		vida -= 9
	elif vida <= 0:
		set_physics_process(false)
		$anim.play("death")
		await get_tree().create_timer(2).timeout
		queue_free()````
if _area.is_in_group("bala"):

If area is in group “bala” you play flash

		flash.play("flash")

and divide 9 from 1 in vida and thats is -8.

		vida -= 9
	elif vida <= 0:

Second time if area is not bala and vida is smaller you destroy node…

The enemy was taking a long time to die so I gave him 1 life and I don’t know why the enemy had to reach the player before dying and I don’t know why the enemy starts blank.


	if _area.is_in_group("bala"): <-- olny if bala
...
	elif vida <= 0:   <-- if not bala and vila 0 or lower , `elif` is mean 'else if'

anything what is not “bala” can trigger second part if “vida” go 0 or lover

Can you explain to me why this is wrong? because I don’t understand very well, I mean I copied the code from another enemy that was chasing another node but I changed the node I was chasing to the player node, by the way, bala is bullet

if bala
else
else mean is not bala.
then mean you is everything but not your bullet

you needed use if inside if

func _on_area_2d_area_entered(_area):
	if _area.is_in_group("bala"):
		flash.play("flash")
		vida -= 1
		if vida <= 0:
			set_physics_process(false)
			$anim.play("death")
			await get_tree().create_timer(2).timeout
			queue_free()

I hope you understand :slight_smile:

Not sure, maybe there’s something in your animations, flash can be active with no script

If it was that, deactivate something and it was fixed, I managed to fix the enemy, thank you very much

1 Like