Author Topic: Health Bar Variant  (Read 2255 times)

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Health Bar Variant
« on: Tue, Jan 1, 2013 »
I was working on a project and needed a zelda-like health bar for my game. I tried using the Power tools cause I remember it having a similar funtion. However, I couldn't get it to behave right. So. I remember playing a Ludum Dare game that had a similar mechanic. So I found it and dug through the source til I found what I needed.

The heart meter:
Code: [Select]
package com
{
import org.flixel.*;
//Kuroki

public class HealthBlock extends FlxSprite
{

[Embed(source='../../data/healthBlock.png')] public var ImgHealth:Class;
public var posX:int;

public function  HealthBlock(X:int, Y:int):void
{
super(X, Y);
loadGraphic(ImgHealth, false, false, 16, 16);
//makeGraphic(16, 16, 0xffFF0000, false, "healthBlock");
posX = X;
}



override public function update():void
{
super.update();
}

override public function draw():void {
//var center:int = FlxG.width ;
                        //Here is where you set the player's health initially
var hearts:int = Registry.health;
                        //The original width of the sprite
var width:int = 16 * hearts + 1;

//y = 229;
frame = 0;
for (var i:uint = 0; i < hearts; i++) {
//x = center - width  + i * 10;
                               //I believe this is the space from the start x, to the next hear's x, if you want spacing between your hearts
x = posX + i *22;
if (Registry.health < i + 1) {
                                        //Er, your guess is as good as mine. 
frame = 1;
}

super.draw();
}
}


}
}



Playstate:
Code: [Select]
//This is how I call it
package com
{
import org.flixel.*;
import org.flixel.plugin.photonstorm.*;
import org.flixel.plugin.photonstorm.FX.StarfieldFX;
import flash.geom.Rectangle;

public class EarthState2 extends FlxState
{
//Music
//[Embed(source= '../../Music/DeadlierWindmills.mp3')] public var AmbMus1:Class;


public var playNow:FlxAdventureButton;

//Mouse
public var mouseCon:FlxSprite;

//Title
public var t:FlxText;

//Tiled background
public var background:FlxSprite;

public var pickBox:FlxAdventureButton;

private var stars:FlxSprite;
private var starfield:StarfieldFX;

public var _earth:Earth;

//The heart meter and it's text box
public var healthG:FlxText = new FlxText(16,0,400,"Health 2:",false);
public var hbar:HealthBlock;

override public function create():void
{
// If the Plugin isn't already in use, we add it here
if (FlxG.getPlugin(FlxScreenGrab) == null)
{
FlxG.addPlugin(new FlxScreenGrab);
}
if (FlxG.getPlugin(FlxSpecialFX) == null)
{
FlxG.addPlugin(new FlxSpecialFX);
}

starfield = FlxSpecialFX.starfield();
stars = starfield.create(0, 0, 640, 480, 256);
add(stars);


FlxG.bgColor = 0xffFFFFFF;

_earth = new Earth(FlxG.width/2-37, FlxG.height/2-37);
add(_earth);

//Health bar
healthG.size = 18;
add(healthG);

//healthG.x *2 + 34,0
//The heart meter attached to it's text box
hbar = new HealthBlock(healthG.x * 2 + 34, 0);
add(hbar);


mouseCon = new FlxSprite(0, 0);
mouseCon.makeGraphic(16, 16, 0xffFFFFFF);
mouseCon.x = mouseCon.y = 1;
//mouseCon.visible = false;
add(mouseCon);

if (Registry.level == 2)
{



}


}
//
override public function update():void
{
//Any action where the player would lose health
if (FlxG.mouse.justPressed())
{

Registry.health--;

}
if(FlxG.keys.any())
{

FlxG.switchState(new EarthState2());
}

super.update();


if (mouseCon.x != FlxG.mouse.x)
{
mouseCon.x = FlxG.mouse.x;
}
if (mouseCon.y != FlxG.mouse.y)
{
mouseCon.y = FlxG.mouse.y;
}


}



}
}

The registry is just your typical registry file. You could probably do without but I don't reccomend it. You can find out how to set one up here: http://www.photonstorm.com/archives/1136/flash-game-dev-tip-1-creating-a-cross-game-communications-structure

Very handy and very easy to do! So I thought I'd share it here.

Originally written by: @arkeus for Arzea for Ludum Dare 22

Enjoy!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Health Bar Variant
« Reply #1 on: Tue, Jan 1, 2013 »
Quote
//Er, your guess is as good as mine.

I lol'd. These weren't my comments were they? :P

Edit: Just for clarification, that makes it so that if you have 6/10 life, the first 6 are stamped with the first frame (full heart), and that line sets the frame to the second frame after the first 6, so the remaining hairs are stamped with an empty heart.

It's worth mentioning that the image used should be a 2 frames, the first being a "filled" heart and the second being empty.
« Last Edit: Tue, Jan 1, 2013 by Arkeus »

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Health Bar Variant
« Reply #2 on: Tue, Jan 1, 2013 »
Heh, nope, those were mine that I added to hopefully explain some things. Thanks for clearing that up though!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha