Add player lifes inside HUD from code

MrToastMrToast Posts: 5Member
edited August 2016 in GUI
I'm trying to make a simple HUD for my game,so I created a Control node (called HUD) and inside I'v inserted a position2d (lifes_holder) but I can't add to the position2d my sprites

This is the HUD node:
Code:
Control2d (HUD)
----Position2d (lifes_holder)
----Position2d (shields_holder)

And this is my code:
Code:
func set_lifes(value):
var texture = preload("res://textures/life.png")
var x = get_node("lifes_holder").get_global_pos().x
var y = get_node("lifes_holder").get_global_pos().y

for i in range(value):
var inst = Sprite.new()
inst.set_texture(texture)
get_node("lifes_holder").add_child(inst)
inst.set_pos(Vector2(x,y))
x += 20
pass

Obviously every time the player lose or gain a life the code remove all the children from the position2d and call the function set_lifes (this is not the best way to  do this but I'm just trying to add the sprites)

Comments

  • WendigoWendigo Posts: 7Member
    An easier way to achieve your life bar would be to use a GridContainer control. Set its "rows" property to the number of "lives" you want displayed in one row and just add or remove TextureFrames (holding your life sprite) as sub scenes. That way the actual positioning is handled automatically.
Sign In or Register to comment.