Introducing AnguarJS Style data bindings for your game's hud.
Demo: http://phaser-hudmanager.herokuapp.com
Usage is simple:
create: function() {
this.score = 0;
var style = { font: '18px Arial', fill: '#ffffff', align: 'center'};
this.hud = Phaser.Plugins.HUDManager.createHud(this.game, this, 'gamehud');
this.scoreHUD = this.hud.addText(x, y, 'Score: ', style, 'score', this);
this.game.add.existing(this.scoreHUD.text);
},
update: function() {
this.score++;
}
The hud manager will watch for changes on the this.score
variable and update your text object so that it will output:
There's also progress/health bars, and general watchers.
returns: a new hud instance. if the name given already exists, it will return the current instance of the hud with that name
Don't create a bunch of hud instances around the game. Instead, create one for your game's hud, and one to encompass all of your sprites, if you're using the health bar component.
By default, the poll rate is once every 100ms. This means the hud dirty checks the watched variables 10 times a second.
x
: (number) the x position of the created Phaser.Text objecty
: (number) the y position of the created Phaser.Text objectlabel
: (string) the string to prepend to the Phaser.Text objectwatched
: (string) the name of the variable to watch in the given contextcontext
: (object) the context of the watched variabled
{ text: Phaser.Text, deregister: Function } Calling the deregistration function will cause the HUDManager instance to stop updating on changes
HUDManager.addBar(x, y, width, height, max, watched, context, color, backgroundColor, shouldCountUp )
Adds a progress/health bar.
x
: (number) the x position of the created Phaser.Sprite objecty
: (number) the y position of the created Phaser.Sprite objectwidth
: (number) the width of the created Phaser.Sprite objectheight
: (number) the height of the created Phaser.Sprite object
watched
: (string) the name of the variable to watch in the given contextcontext
: (object) the context of the watched variabledcolor
: (string | function) the color of the created bar. You can provide a function that accepts (percent) as an argument. Percent will be a value between 0 and 1 indicating how much of the bar is filled. The function must return a string that represents a color. DEFAULT: 'white'- 'backgroundColor': (string) A string representing a color. DEFAULT: '#999'
{ bar: Phaser.Sprite, deregister: Function } Calling the deregistration function will cause the HUDManager instance to stop updating on changes
This is a predefined function that you can pass as the color
argument in HUDManager.addBar
. This will automatically return a green, yellow, or red color to signify the health amount.
watched
: (string) the name of the variable to watch in the given contextwatchedContext
: (object) the context of the watched variabledcallback
: (Function(newValue, oldValue)) callback to call when a change has been detectedcallbackContext
: (object) the context of the callback function
The returned function is the deregistration function. Calling this function will stop the HUDManager instance from watching for changes on this watched item.