Discuss Scratch

GameFanScratch11
Scratcher
17 posts

Health Bar

if <answer> then
...//I need help
end
Deivos
Scratcher
62 posts

Health Bar

GameFanScratch11 wrote:

StampDanFan wrote:

I think you should use costumes, as it is easier and more beginner-friendly.

First of all, you need two variables, Health, and Maxhealth.
Maxhealth is the maximum health the object has, and health will be the current amount of health the object has.
Let's create a new sprite called “HealthBar”, and these two new variables.
set [Maxhealth v] to [10]
set [Health v] to (maxhealth)

Create 6 costumes. Make one a health bar that is empty, 1/5, 2/5, 3/5, 4/5, and full.
Rename the costumes to 0, 1, 2, 3, 4, 5.
It should look something like this:
https://ibb.co/LnMjRxj

Then, every frame of the game, you want to update the costume of the sprite to accurately show the health.
Use this code:
switch costume to (round(((Health) / (Maxhealth)) * (5)))
It will calculate the fraction of health the character has, scale it up to the costume number, and then round it to an integer.
You can use this in a forever loop to make a hotbar.

If you want to detect if a character is dead, use this:
if <<(health) = (0)> or <(health) < (0)>> then
... //Do what you want in here
end

Hope that this helped!
what should i put inetween if?
basically put what you want to happen when the person dies

Oh look a signature.
























No
StampDanFan
Scratcher
100+ posts

Health Bar

GameFanScratch11 wrote:

StampDanFan wrote:




If you want to detect if a character is dead, use this:
if <<(health) = (0)> or <(health) < (0)>> then
... //Do what you want in here
end

Hope that this helped!
what should i put inetween if?

It depends on the game, but you should probably send a reset broadcast, that sprites will react to when the player dies.
It could trigger a game over screen, or move the player back to its spawn location.

Completely up to you and the game you're making.

StampDanFan ( Scratch Wiki | Dodge the Coronavirus )

if <I helped you ::operators> then
Follow [StampDanFan v] ::motion
end
MasterofTheBrick
Scratcher
1000+ posts

Health Bar

GameFanScratch11 wrote:

StampDanFan wrote:

I think you should use costumes, as it is easier and more beginner-friendly.

First of all, you need two variables, Health, and Maxhealth.
Maxhealth is the maximum health the object has, and health will be the current amount of health the object has.
Let's create a new sprite called “HealthBar”, and these two new variables.
set [Maxhealth v] to [10]
set [Health v] to (maxhealth)

Create 6 costumes. Make one a health bar that is empty, 1/5, 2/5, 3/5, 4/5, and full.
Rename the costumes to 0, 1, 2, 3, 4, 5.
It should look something like this:
https://ibb.co/LnMjRxj

Then, every frame of the game, you want to update the costume of the sprite to accurately show the health.
Use this code:
switch costume to (round(((Health) / (Maxhealth)) * (5)))
It will calculate the fraction of health the character has, scale it up to the costume number, and then round it to an integer.
You can use this in a forever loop to make a hotbar.

If you want to detect if a character is dead, use this:
if <<(health) = (0)> or <(health) < (0)>> then
... //Do what you want in here
end

Hope that this helped!
what should i put inetween if?

He already said it: Do whatever you want in there, whether it is switching your character's costume to a ‘dead’ one or changing the healthbar to a colour such as red.

Last edited by MasterofTheBrick (June 10, 2021 08:47:52)

Powered by DjangoBB