Discuss Scratch

Slam-
Scratcher
100+ posts

health bar

how to create simple healthbar whout pen (with clons)?
deck26
Scratcher
1000+ posts

health bar

Create, for example, ten clones. Give each a cloneID (local variable, set by sprite before it creates each clone) and only show the clone if the health is at least as high as its value. Move the sprite a few pixels each time you create a clone so they are spaced correctly.

Alternatively each clone could have, for example, a green block costume and a red block costume and show the red one if health is lower than cloneID, otherwise the green one.

Play-pause-games
Scratcher
100+ posts

health bar

an simpler and more effecient solution would be to creat a sprite with say 10 costume for 10 states o fthe hp bar
then create a variable and make the sprites code like this

whenclickedset(hp)to10foreverswitchcostumeto(hp)

then make sure the first costume is the least full health and the last is the most full - so it displays properly

i use this in a lot of my project so im sure you can find an example there
deck26
Scratcher
1000+ posts

health bar

I agree @Play-pause-games solutiont is simpler, I was distracted by you mentioning clones.
Slam-
Scratcher
100+ posts

health bar

I have tried…
whenclickedshowset hpto10set max hpto10repeatmaxhpcreatecloneof sprite1setxto10
whenIstartasacloneshow
Further I do not know …

Last edited by Slam- (Aug. 30, 2018 03:13:05)

deck26
Scratcher
1000+ posts

health bar

Why not go for @Play-pause-games solution?

You haven't given each clone a unique cloneID which is essential for my version. Set cloneID to 1 and after you create each clone change the sprite's copy of the variable (it has to be for this sprite only so each clone inherits the value the sprite's copy had when the clone was created).
Slam-
Scratcher
100+ posts

health bar


whenclickedshowset hpto10set max hpto10set clone IDto1repeatmaxhpcreatecloneof sprite1change clone IDby1setxto10
whenIstartasacloneshowifhp<Clone IDthenhide

Last edited by Slam- (Aug. 30, 2018 10:34:21)

Nuclearpuppy
Scratcher
36 posts

health bar

Make a health bar sprite with 10 costumes (lets say 100 is the most hp you can have). Whenever the heath is, say, >90 and <100, the costume is costume 2 (with one less interval). When the health variable is >80 and <90, switch to costume 3 and so on. Base it all off Play-pause-games' script and expand from there.
deck26
Scratcher
1000+ posts

health bar

Slam- wrote:

whenclickedshowset hpto10set max hpto10set clone IDto1repeatmaxhpcreatecloneof sprite1change clone IDby1setxto10
whenIstartasacloneshowifhp<Clone IDthenhide

That should be close but try changing x by 10 instead of setting it to 10. All your clones are in one place.
Slam-
Scratcher
100+ posts

health bar

When the health changes, all clones are hidden

RokCoder
Scratcher
1000+ posts

health bar

This was my example project for a similar question. It uses a curved health bar but the same principle could be used for a standard health bar (but sliding the mask sprite across the health bar rather than rotating it as in the example project). It's not better or worse than the previously suggested solutions - just another different option.
Slam-
Scratcher
100+ posts

health bar

Not bad, but this health bar will not work for my project.
My script, as it seems to me, is closest to the desired result. But there is a problem, with a change in health, all clones hiding
_nix
Scratcher
1000+ posts

health bar

Slam- wrote:

Not bad, but this health bar will not work for my project.
My script, as it seems to me, is closest to the desired result. But there is a problem, with a change in health, all clones hiding
That's because once the sprite hides, it is never told to show again. You might want to do something like this instead, in the “when I start as a clone” block:

whenIstartasacloneforeverifhp<cloneIDthenhideelseshow
deck26
Scratcher
1000+ posts

health bar

_nix wrote:

Slam- wrote:

Not bad, but this health bar will not work for my project.
My script, as it seems to me, is closest to the desired result. But there is a problem, with a change in health, all clones hiding
That's because once the sprite hides, it is never told to show again. You might want to do something like this instead, in the “when I start as a clone” block:

whenIstartasacloneforeverifhp<cloneIDthenhideelseshow
Better to use wait untils so you're not endlessly telling Scratch to show the sprite when it's already showing.

forever
show
wait until hp < clone ID
hide
wait until not hp < clone ID
end

Powered by DjangoBB