![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Mhd08 |
i have this enemy code and i am trying to create a hp bar that is visible only when get hits and i have no idea how
here is the enemy code :
extends KinematicBody2D
var velocity = Vector2(50,0)
var gravity = 15
var turned_side
var health = 100
func _ready():
$AnimatedSprite.play("move")
$CollisionShape2D.position.y == 0
$hpbar.value = health
func _physics_process(_delta):
velocity.y += gravity
move_and_slide(velocity,Vector2.UP)
func change_state():
velocity.x *= -1
scale.x *= -1
if velocity.x < 0:
turned_side = true
elif velocity.x > 0:
turned_side = false
func _on_detect_player_body_entered(body):
if body.is_in_group("player"):
$AnimatedSprite.play("attack")
velocity = Vector2(0,0)
func _on_detect_player_body_exited(body):
if body.is_in_group("player"):
$AnimatedSprite.play("move")
if turned_side == true:
velocity.x = -50
elif turned_side == false:
velocity.x = 50
func _on_attack_player_body_entered(body):
if body.is_in_group("player"):
body.attack_detected(20)
func _on_player_is_back_body_entered(body):
if body.is_in_group("player"):
change_state()
func attacked(damage):
health -= damage
$hpbar.value = health
if health == 0:
$AnimatedSprite.play("die")
$Timer.wait_time = 2
$Timer.start()
func _on_Timer_timeout():
queue_free()
Edited to fix code indentation.
Zylann | 2023-07-06 12:38