Easy hp bar code


(billy2000) #1

I’ve been looking around the forum but couldn’t find a easier method of adding hp bars in my game then the one it was on the old forum.All the credit goes to DespondentDonkey. The code looks like this: (StatuBar.as)

package  
{


import net.flashpunk.Entity;
import net.flashpunk.*;
import net.flashpunk.graphics.*;

public class StatusBar extends Entity
{	
public var bar:Image;
public var tick:Image;
public var ratio:Number;
public function StatusBar(barImage:Image, tickImage:Image, x:int = 0, y:int = 0, layer:int = -4)
{			
	bar = barImage;
	bar.centerOrigin();
	
	super(x, y, bar);
	
	tick = tickImage;
        tick.scaleY=-1;
	tick.centerOrigin();
	addGraphic(tick);
	
	this.layer = layer;
	
}

override public function update():void 
{
          tick.clipRect.height = (ratio) * bar.height;
	  tick.updateBuffer(true);
	
}
}
}

And in world class:

private var YourHpBar:StatusBar = new StatusBar( new Image(TheHpBarImage), new Image(TheLiquidImage),  HpBarX, HpBarY, HpBarLayer);

And all you have to do is to change YourHpBar.ratio as u wish(should be between 0 and 1).

If you whant to play around with the bar ,to make the liquid drain from the top/bottom or to make a verticaly bar, just change YourHpBar.tick.scaleY or for the other case in the StatusBar update instead of height change it to width.

Hope this helps :smile:


(billy2000) #2

Short edit. The code made liquid drain from bottom to top so i added a little scaleY=-1 :smile: