The current version is kh2Bar 2.0 released July 10, 2013. A demo project and screenshot are attached.
[gist]5449762[/gist]
Changelog:
v2.0
- New maxSectors argument for kh2Bar() constructor, useful if you need to guarantee a gauge can fit a certain number of reserve sectors.
- New combo functionality: Call .beginCombo() to start a combo. Any damage sustained during the combo won't fade away until .endCombo() is called.
- New .changeColor method to adjust the color of the gauge after creation.
- Fixed long-standing glitchiness when recovering HP.
- Gauge now drains smoothly when reading is changed.
- Added checks for correct number of arguments to user-facing methods.
v1.6.1:
- Updated fill logic so bar is never empty if HP is nonzero
v1.6:
- Lots of refactoring.
- Renamed render() to draw().
- New show/hide functionality with optional fade in/out. Gauge is hidden upon creation.
v1.5.1:
- Fixed a few off-by-one errors in kh2Bar.render() that were causing the gauge to be filled inconsistently depending on the amount of damage displayed.
v1.5:
- Updated presentation: The new version is much more faithful to the KH2 design. The main lifebar is right-aligned, and if the bar on top is only a partial (as when the gauge is full and maxHP isn't an exact multiple of the sector size), the bar underneath shows through.
- Constructor no longer takes x and y parameters; these have been moved to kh2Bar.render().
- Changed 'reading' property to plain methods as getter/setter methods are SpiderMonkey-specific.
- The size of the gauge is no longer hard-coded: kh2Bar.render() now also takes 'width' and 'height' as parameters.
- Sector size (HP per bar) is now customizable via an optional constructor parameter. The default is 100.
- Color of the gauge is also customizable. The default color is green (#00FF00 @ 100% opacity), as in KH.
v1.0:
Usage:
For each kh2Bar you have on-screen, you'll have to call its .update() and .draw() methods once per frame.
RequireScript("kh2Bar.js");
// maxHP is required, sectorSize and color are optional. If not provided, the default values are
// sectorSize: 100 (100 HP per bar) and color: #00FF00 (green) @ 100%.
var lifebar = new kh2Bar(maxHP, sectorSize, color);
lifebar.show(); // bar is initially hidden, must call show() or the player won't see it
// Inside your game loop:
lifebar.update();
lifebar.draw(x, y, width, height);
Whenever the amount of HP changes and you want the lifebar to reflect it:
lifebar.set(currentHP);
If you need to hide the gauge temporarily (for a cutscene, etc.) just call hide(), and later show() when you need it to be visible again. Both take an optional parameter specifying the amount of time over which to fade it in/out.