Need help making a healthbar


(Calem Young) #1

Hello everyone, I’m currently making a game and need help making a health bar. What I want is two rectangles on red one and one green one on top. I want it so that when the variable ‘health’ is decreased, the green rectangle gets shorter to one side, showing the red bar underneath. I want it so that when the variable health = 0 the green bar will completely disappear revealing the whole red bar underneath. I want it so this health bar is it’s own class, not a shape rendered in the world class becuase I need to make multiple healthbars.

Any help is very much appreciated :smile:


(rostok) #2

class:

	public class Healthbar extends Entity 
	{
		private var ent:Entity;
		private var variable:String;
		
		public function Healthbar(x:Number, y:Number, w:Number, h:Number, ent:Entity, variable:String) 
		{
			this.x = x;
			this.y = y;
			setHitbox(w, h);
			this.ent = ent;
			this.variable = variable;
			layer = -10000;
		}
		
		override public function render():void 
		{
			Draw.rect(world.camera.x+x, world.camera.y+y, width, height, 0xFF0000);
			Draw.rect(world.camera.x+x, world.camera.y+y, width*ent[variable], height, 0x00FF00);
		}
	}

add & init

add(new Healthbar(5, 5, 100, 5, getInstance("player"), "health"));

(Calem Young) #3

I found I needed to do a few tweaks to get your code working but it really helped. Thanks SOOOOO much :smile: