Discuss Scratch

springtrap203
Scratcher
5 posts

how to make a health bar

So I want to add a health bar for a enemies in my game but don't know how to add them, also why is it when my enemie touches me he wont use his power, i put the command

Last edited by springtrap203 (Feb. 27, 2020 20:46:52)

DrGaming_92Games
Scratcher
100+ posts

how to make a health bar

A way to do this would to make a new sprite that will be the enemies' health bars. Have several costumes in this sprite that are named amounts of health so that once the enemy has that amount of health it will switch to the costume corresponding with the amount of health the enemy has. Note that you don't have to have a costume for every possible value for the enemy health, you can just have it change when the enemy health is divisible by a certain number, example:

if <((health) mod (5)) = [0]> then
switch costume to (health)
end

In the code above, the health bar would only change in increments of five. Make sure each costume you have been named accordingly. Some more work needs to be done to make sure this functions with a specific sprite: you need to store the enemies' health not in variable but in a list. Give each of the clones a clone ID and store them in a list. To refer to the health stat of a specific clone use this
(item (item # of (enemy ID) in [enemy IDs v] :: list) of [enemy health v])
This will refer to the health of the enemy based of the index of the clone ID in the list of enemy IDs, so make sure that you add both the ID and the health stat to their respective lists at the same time. To have the health bar in the position that you want it in (in this example somewhere above the enemy's head) you would use something like this:

repeat until <(item (item # of (enemy ID) in [enemy IDs v] :: list) of [list v] :: list) = [0]>
go to x: (item (item # of (enemy ID) in [enemy IDs v] :: list) of [enemy X v]) y: ((item (item # of (enemy ID) in [enemy IDs v] :: list) of [enemy X v]) + (45))
end
delete this clone

Note that to do this you will also need to make stats for the enemies' X position and Y position, I suggest adding them into separate lists. You will also have to regularly update the position of the enemy to get the health bar to be over the enemy. Make sure to delete your clones once an enemy has died, both the enemy clone and the health bar clone.

I realize that this may seem confusing, so if you have any questions feel free to ask. Also, it would be helpful to post the link to your project in this topic so that I can get a better look at it and give you more advice specialized for your project. Hope this helped!

Last edited by DrGaming_92Games (Feb. 28, 2020 13:58:27)

springtrap203
Scratcher
5 posts

how to make a health bar

DrGaming_92Games wrote:

A way to do this would to make a new sprite that will be the enemies' health bars. Have several costumes in this sprite that are named amounts of health so that once the enemy has that amount of health it will switch to the costume corresponding with the amount of health the enemy has. Note that you don't have to have a costume for every possible value for the enemy health, you can just have it change when the enemy health is divisible by a certain number, example:

if <((health) mod (5)) = [0]> then
switch costume to (health)
end

In the code above, the health bar would only change in increments of five. Make sure each costume you have been named accordingly. Some more work needs to be done to make sure this functions with a specific sprite: you need to store the enemies' health not in variable but in a list. Give each of the clones a clone ID and store them in a list. To refer to the health stat of a specific clone use this
(item (item # of (enemy ID) in [enemy IDs v]) of [enemy health v])
This will refer to the health of the enemy based of the index of the clone ID in the list of enemy IDs, so make sure that you add both the ID and the health stat to their respective lists at the same time. To have the health bar in the position that you want it in (in this example somewhere above the enemy's head) you would use something like this:

repeat until <(item (item # of (enemy ID) in [enemy IDs v]) of [list v] :: list) = [0]>
go to x: (item (item # of (enemy ID) in [enemy IDs v]) of [enemy X v]) y: ((item (item # of (enemy ID) in [enemy IDs v]) of [enemy X v]) + (45))
end
delete this clone

Note that to do this you will also need to make stats for the enemies' X position and Y position, I suggest adding them into separate lists. You will also have to regularly update the position of the enemy to get the health bar to be over the enemy. Make sure to delete your clones once an enemy has died, both the enemy clone and the health bar clone.

I realize that this may seem confusing, so if you have any questions feel free to ask. Also, it would be helpful to post the link to your project in this topic so that I can get a better look at it and give you more advice specialized for your project. Hope this helped!


thanks
Highstone
Scratcher
68 posts

how to make a health bar

easy other solution:
forever
if <touching [player sprite v] ?> then
broadcast [player health v]

end
end

when I receive [player health v]
switch costume to [one lower health v]
Highstone
Scratcher
68 posts

how to make a health bar

Oh. you should either move the sprite right after or use a
wait () secs
block to wait before it goes down again.
springtrap203
Scratcher
5 posts

how to make a health bar

Highstone wrote:

Oh. you should either move the sprite right after or use a
wait () secs
block to wait before it goes down again.
thanks
Ntnathanbeast11
Scratcher
1 post

how to make a health bar

@DrGaming_92Games i need help on making healthbar in my game can you help me pls?/
Here is the game: https://scratch.mit.edu/projects/896024728/
hope you can help
subjectnamehere
Scratcher
1000+ posts

how to make a health bar

Here's a pretty good tutorial by griffpatch on it: https://scratch.mit.edu/discuss/youtube/rTKDzz_d6X4

Last edited by subjectnamehere (Sept. 26, 2023 10:55:15)

tomthenewrahster
Scratcher
2 posts

how to make a health bar

just a random thing of code


Last edited by tomthenewrahster (Jan. 8, 2025 21:32:32)

DragonPlusMaster
Scratcher
100+ posts

how to make a health bar

tomthenewrahster wrote:

just a random thing of code


Please do not necropost (post on an old topic).
cosmosaura
Scratch Team
1000+ posts

how to make a health bar

Topic closed due to necroposting.

Powered by DjangoBB