O'Reilly logo

Haxe Game Development Essentials by Jeremy McCurdy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Setting health on the HUD

Since we now have a character that will take damage, we need a function to update the health display.

Changing hearts

We just need to add one small function to the GameHUD class:

public function setHealth(health:Int):Void{
for (i in 0...healthHearts.length) {
  if(i > (health -1)){
     healthHearts[i].loadGraphic(AssetPaths.healthHeartOff__png);
    }
  }
}

This is a public function that takes in an integer health value.

Inside the function, we loop through each of the hearts on the HUD and check whether its index is greater than the amount of health that was passed in. If it is, then we load the off version of the heart graphic into that heart sprite.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required