Skip to content

Commit 8cf28fe

Browse files
committed
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.
1 parent b725c25 commit 8cf28fe

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ Version 2.4 - "Katar" - in dev
314314
* Device.electron will return true if running under GitHub Electron (thanks @rblopes #1851)
315315
* 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)
316316
* 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)
317+
* 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.
317318

318319
### Updates
319320

src/gameobjects/components/Health.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ Phaser.Component.Health.prototype = {
1919
* The Game Objects health value. This is a handy property for setting and manipulating health on a Game Object.
2020
*
2121
* It can be used in combination with the `damage` method or modified directly.
22+
*
2223
* @property {number} health
2324
* @default
2425
*/
2526
health: 1,
2627

28+
/**
29+
* The Game Objects maximum health value. This works in combination with the `heal` method to ensure
30+
* the health value never exceeds the maximum.
31+
*
32+
* @property {number} maxHealth
33+
* @default
34+
*/
35+
maxHealth: 100,
36+
2737
/**
2838
* Damages the Game Object. This removes the given amount of health from the `health` property.
2939
*
@@ -50,17 +60,22 @@ Phaser.Component.Health.prototype = {
5060
},
5161

5262
/**
53-
* Heal the Game Object. This adds the given amount of health from the `health` property.
63+
* Heal the Game Object. This adds the given amount of health to the `health` property.
5464
*
5565
* @member
56-
* @param {number} amount - The amount to add from the current `health` value.
66+
* @param {number} amount - The amount to add to the current `health` value. The total will never exceed `maxHealth`.
5767
* @return {Phaser.Sprite} This instance.
5868
*/
5969
heal: function(amount) {
6070

6171
if (this.alive)
6272
{
6373
this.health += amount;
74+
75+
if (this.health > this.maxHealth)
76+
{
77+
this.health = this.maxHealth;
78+
}
6479
}
6580

6681
return this;

typescript/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,6 +4059,7 @@ declare module Phaser {
40594059
key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture;
40604060
left: number;
40614061
lifespan: number;
4062+
maxHealth: number;
40624063
name: string;
40634064
offsetX: number;
40644065
offsetY: number;
@@ -4087,6 +4088,7 @@ declare module Phaser {
40874088
damage(amount: number): Phaser.Sprite;
40884089
destroy(destroyChildren?: boolean): void;
40894090
drawPolygon(): void;
4091+
heal(amount: number): Phaser.Sprite;
40904092
kill(): Phaser.Sprite;
40914093
loadTexture(key: string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture, frame?: string|number, stopAnimation?: boolean): void;
40924094
overlap(displayObject: PIXI.DisplayObject): boolean;

0 commit comments

Comments
 (0)