maxHealth is a new property that Game Objects with the Health component receive and works in combination with the heal method to ensure a health limit cap.

This commit is contained in:
photonstorm 2015-06-17 02:00:04 +01:00
parent b725c25702
commit 8cf28fede7
3 changed files with 20 additions and 2 deletions

View file

@ -314,6 +314,7 @@ Version 2.4 - "Katar" - in dev
* Device.electron will return true if running under GitHub Electron (thanks @rblopes #1851)
* When loading a BitmapText you can now specify either an XML file or a JSON file for the font data. This is useful in environments such as Cocoon where you don't have a native XML parser. If you wish to use JSON the formatting should be equal to the result of running a valid XML file through X2JS (thanks @Feenposhleen #1837)
* Game Objects that have the Health component (such as Sprites) now have a new method: `heal` which adds the given amount to the health property, i.e. is the opposite of `damage` (thanks @stephandesouza #1794)
* maxHealth is a new property that Game Objects with the Health component receive and works in combination with the `heal` method to ensure a health limit cap.
### Updates

View file

@ -19,11 +19,21 @@ Phaser.Component.Health.prototype = {
* The Game Objects health value. This is a handy property for setting and manipulating health on a Game Object.
*
* It can be used in combination with the `damage` method or modified directly.
*
* @property {number} health
* @default
*/
health: 1,
/**
* The Game Objects maximum health value. This works in combination with the `heal` method to ensure
* the health value never exceeds the maximum.
*
* @property {number} maxHealth
* @default
*/
maxHealth: 100,
/**
* Damages the Game Object. This removes the given amount of health from the `health` property.
*
@ -50,10 +60,10 @@ Phaser.Component.Health.prototype = {
},
/**
* Heal the Game Object. This adds the given amount of health from the `health` property.
* Heal the Game Object. This adds the given amount of health to the `health` property.
*
* @member
* @param {number} amount - The amount to add from the current `health` value.
* @param {number} amount - The amount to add to the current `health` value. The total will never exceed `maxHealth`.
* @return {Phaser.Sprite} This instance.
*/
heal: function(amount) {
@ -61,6 +71,11 @@ Phaser.Component.Health.prototype = {
if (this.alive)
{
this.health += amount;
if (this.health > this.maxHealth)
{
this.health = this.maxHealth;
}
}
return this;

View file

@ -4059,6 +4059,7 @@ declare module Phaser {
key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture;
left: number;
lifespan: number;
maxHealth: number;
name: string;
offsetX: number;
offsetY: number;
@ -4087,6 +4088,7 @@ declare module Phaser {
damage(amount: number): Phaser.Sprite;
destroy(destroyChildren?: boolean): void;
drawPolygon(): void;
heal(amount: number): Phaser.Sprite;
kill(): Phaser.Sprite;
loadTexture(key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture, frame?: string|number, stopAnimation?: boolean): void;
overlap(displayObject: PIXI.DisplayObject): boolean;