Discuss Scratch

KILLxSCAPE
New to Scratch
11 posts

Health bar code

I’m working on an RPG game and I need a health bar do you think this code would work and if so how would I define a When variable reaches block
when backdrop switches to [ Fighting backdrop]
set [ HP] to [5]
if <touching [ enemy is touching sprite] ?> then
change [ HP]by (-1)
end

when backdrop switches to [ Fighting backdrop]
if <Variable reaches 0> then
hide variable [ HP]
switch costume to [ Game over]
end

Last edited by KILLxSCAPE (Dec. 16, 2021 02:31:24)


ThE bEsT cOdEr No CaP
Jareddddddd
Scratcher
1000+ posts

Health bar code

yes this would theoretically work
tip in the future - instead of creating new posts, bump your old ones simply by replying “bump”
(bumping it puts the post at the top of the list)
KILLxSCAPE
New to Scratch
11 posts

Health bar code

Jareddddddd wrote:

yes this would theoretically work
tip in the future - instead of creating new posts, bump your old ones simply by replying “bump”
(bumping it puts the post at the top of the list)
Thanks for the tip didn’t know that!

ThE bEsT cOdEr No CaP
deck26
Scratcher
1000+ posts

Health bar code

An easier option is to check if HP is 0 in the same script that changes it. Neither of your scripts has a loop so they won't work. The backdrop switches, you set HP to 5 and only in that split second do you check for touching so HP only has one very brief chance to be changed.

More likely you want something like this

when backdrop switches to [ v]
set [HP v] to [5]
repeat until <(HP) < [1] >
wait until <touching [ v] ?>
change [HP v] by [-1]
wait until <not <touching [ v] ?>>
end
hide variable [HP v]
switch costume to [GameOver v]
You want to wait until not touching or wait 0.5 secs or something so a single touch doesn't change HP multiple times since the loop will run around 30 times a second. It is generally best to check for HP < a value rather than equal to although in this case it doesn't matter - but think of an example where HP is being changed elsewhere and two sprites/clones change HP between checks. In that case it might go from 1 to -1 so checking for HP=0 would fail.
KILLxSCAPE
New to Scratch
11 posts

Health bar code

deck26 wrote:

An easier option is to check if HP is 0 in the same script that changes it. Neither of your scripts has a loop so they won't work. The backdrop switches, you set HP to 5 and only in that split second do you check for touching so HP only has one very brief chance to be changed.

More likely you want something like this

when backdrop switches to [ v]
set [HP v] to [5]
repeat until <(HP) < [1] >
wait until <touching [ v] ?>
change [HP v] by [-1]
wait until <not <touching [ v] ?>>
end
hide variable [HP v]
switch costume to [GameOver v]
You want to wait until not touching or wait 0.5 secs or something so a single touch doesn't change HP multiple times since the loop will run around 30 times a second. It is generally best to check for HP < a value rather than equal to although in this case it doesn't matter - but think of an example where HP is being changed elsewhere and two sprites/clones change HP between checks. In that case it might go from 1 to -1 so checking for HP=0 would fail.

I didn’t really think about that thanks for the advice!

ThE bEsT cOdEr No CaP

Powered by DjangoBB