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<br /><br />This is the HUD node:<br />
<br />Control2d (HUD)<br />----Position2d (lifes_holder)<br />----Position2d (shields_holder)<br />
<br /><br />And this is my code:<br />
<br />func set_lifes(value):<br />	var texture = preload(&quot;res://textures/life.png&quot;)<br />	var x = get_node(&quot;lifes_holder&quot;).get_global_pos().x<br />	var y = get_node(&quot;lifes_holder&quot;).get_global_pos().y<br />	<br />	for i in range(value):<br />		var inst =<br />		inst.set_texture(texture)<br />		get_node(&quot;lifes_holder&quot;).add_child(inst)<br />		inst.set_pos(Vector2(x,y))<br />		x += 20<br />	pass<br />
<br /><br />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)


  • WendigoWendigo Posts: 8Member
    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.