How to create a health bar

Objective — create an image that changes in size based on your health.

First thing would be to grab an image that we will use for our sprite. In your sprite, make sure you set the image type to filled and horizontal. Now we have a bar that fills from left to right. You can adjust the fill origin as necessary.

In my player class, I’m going to create a handle that connects to my UI Manager. This function is going to run another function in my UI manager called HealthBar(). I’m also going to pass an int value through it, which is my health int value.

In my UI manager, I’m going to connect to my health bar image.

Finally, I’m going to create this function that serves as logic. I dislike switch statements. I know if statements take a little longer to write, but if they deal with numerical values, I prefer if statements. Basically, numbers 0–5 have a float value, and that’s how big the bar should be.

Now whenever I get hit, the function runs and the health bar adjusts to the appropriate value. If the value ever goes over 5 or under 0, it goes back to 5.