Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | javrocks |
Below is the code for my TextureRect which is a child of the Enemy. Whenever a body enters the hurt box, my hitpoints variable inconsistently switches between values 50, -50, 100 and 0 causing the Enemy to be remove from the scene tree at random
I think something is wrong with the logic or maybe there an easier way to make a Health Bar
extends TextureRect
func _ready():
$TextureProgress.value = 100
func set_percent_value_int(value):
$TextureProgress.value = value
extends KinematicBody2D #The Enemy
export(int) var hitpoints = 100
var max_hitpoints = 100
func _on_Hurtbox_body_entered(body):
hitpoints -= 50
$TextureRect.set_percent_value_int(float(hitpoints)/max_hitpoints * 100)
if hitpoints <= 0:
queue_free()
print (hitpoints)