Health Bar
Introduction
Last updated
Introduction
Last updated
Health bars are common UI elements in games. They are similar to progress bars, so this example could be used to create either. This example explains how to create a health bar component.
First we'll define the component:
Open Gum
Open or create a new Gum project
Right-click on the Components folder
Name the component "HealthBar"
Resize the HealthBar component so it is wider than it is tall. For example, assign Width
to 200
and Height
to 32
.
Next we'll add a background to our HealthBar Component
Drag+drop a ColoredRectangle into the HealthBar
Select the newly-created ColoredRectangleInstance
Select the Alignment tab
Click the Fill Dock button
Change the ColoredRectangleInstance color to black
Now we have a black background in our HealthBar
The HealthBar displays its current health with another rectangle. This second rectangle will be contained inside a container to provide a boundary. To add an inner container:
Drag+drop a Container onto the HealthBar
Select the Alignment tab
Enter a Margin value of 4
Click the Fill Dock button
Now we have a ContainerInstance with the proper margin
Finally we'll add the foreground rectangle which displays the health:
Drag+drop another ColoredRectangle onto the ContainerInstance. Be sure to drop it on ContainerInstance so that the newly-added ColoredRectangle is a child of the container
Click the Alignment tab
Set Margin back to 0
Click the Fill Dock button
Change the following values:
X Units to Pixels from Left
X Origin to Left
Width to Percentage of Container
Width to 100
Now, the Width value can change between 0 and 100 to indicate the health percentage.
Next we'll expose the inner ColoredRectangle's Width
property so it can be assigned per HealthBar instance:
Select the inner ColoredRectangle instance
Right-click on its Width
variable and select Expose Variable
Enter an appropriate name such as "Percentage" and click OK
Now we can add instances of the HealthBar to a screen and control its fill percentage.