Discuss Scratch

Epic_Blue_Cat6412
Scratcher
36 posts

How do I code health?

I'm trying to make a game where if the sword touches the enemy, the enemy's health will go down. What code can I use?

Also, how do you make the sprite come to the player, but bounces off the player so that they don't overlap? Basically, I want to make the enemy solid.
Catscratcher07
Scratcher
1000+ posts

How do I code health?

make the enemy's heath a variable (you will need for this sprite only if there will be mutable enemies) and:
   
if <touching [sword v] ?> then
change [health v] by (negative number)
wait until <not <touching [sword v] ?>> //optional, use if you want the sword to do one hit of damage rather than damage every frame.
end
as for the enemy being solid:
if <touching [player v] ?> then
move (negative number) steps
end
you might consider putting a wait zero seconds before the movement, so it doesn't trigger before the player's take damage script activates.

trouble with clones? view my clone id toolbox! I also have built the bases of a tower defence game link here.
If someone on help with scripts is saying that something can't be done, they should be suspected of being a team limit grunt.
mra26b25c2
Scratcher
23 posts

How do I code health?

Health: You could use a local Variable for each enemy (Every clone has an own local variable, they aren't the same.). Then simply decrease this in the script of the clone. You could make multiple costumes for some health statuses (e.g. 100%, 90%, …, 10%) and apply them in terms of the health.
Solid player: You may use something like this:
point towards [player v]
if ((distance to [player v]) > (50)) then
move (5) steps
end
Catscratcher07
Scratcher
1000+ posts

How do I code health?

mra26b25c2 wrote:

~snip~
Solid player: You may use something like this:
point towards [player v]
if ((distance to [player v]) > (50)) then
move (5) steps
end
that would cause the enemy to move five steps so long as they are more than 50 steps from the player, that's not what is being asked for.

trouble with clones? view my clone id toolbox! I also have built the bases of a tower defence game link here.
If someone on help with scripts is saying that something can't be done, they should be suspected of being a team limit grunt.
mra26b25c2
Scratcher
23 posts

How do I code health?

Catscratcher07 wrote:

that would cause the enemy to move five steps so long as they are more than 50 steps from the player, that's not what is being asked for.
Well, it depends. If the player can move your script may be better because mine doesn't work. But if the player can't move (that's what I thought) I think mine is better. Yours would make the enemies go forward and backwards and so on. Mine doesn't allow the player to to touch the player.
If the player is moving, I think the best solution would be something like our scripts combined:
point towards [player v]
if ((distance to [player v]) > (50)) then
move (5) steps
end
if <touching [player v] ?> then
move (-5) steps
end
Epic_Blue_Cat6412
Scratcher
36 posts

How do I code health?

How do I make it so that when the game starts the health of the enemy is set to 50? Currently I have this:

when green flag clicked
set [health v] to [50]

Also, do you know how to code a health bar?
Epic_Blue_Cat6412
Scratcher
36 posts

How do I code health?

mra26b25c2 wrote:

Catscratcher07 wrote:

that would cause the enemy to move five steps so long as they are more than 50 steps from the player, that's not what is being asked for.
Well, it depends. If the player can move your script may be better because mine doesn't work. But if the player can't move (that's what I thought) I think mine is better. Yours would make the enemies go forward and backwards and so on. Mine doesn't allow the player to to touch the player.
If the player is moving, I think the best solution would be something like our scripts combined:
point towards [player v]
if ((distance to [player v]) > (50)) then
move (5) steps
end
if <touching [player v] ?> then
move (-5) steps
end

When I try to make it point to the player (the characters are simple cubes with hats) it just makes it go everywhere.
I'm also trying to make knockback so the enemy doesn't stay in one place when hit, as well as trying to code it so that the enemy moves toward the player, and when it dies it clones itself.
medians
Scratcher
1000+ posts

How do I code health?

Epic_Blue_Cat6412 wrote:

How do I make it so that when the game starts the health of the enemy is set to 50? Currently I have this:

when green flag clicked
set [health v] to [50]

Also, do you know how to code a health bar?
You could do sth like this:
when I receive [start game v] 
set [health v] to [50]
And then to send the message:
broadcast [start game v] //broadcasts are useful when telling other sprites to do something
For a health bar, you can use pen, costumes, just simply show the health on the screen, or sth (note pen always goes behind the sprites)

Last edited by medians (Jan. 4, 2024 20:55:23)


NEW: Medians bamboozled by 3.0 (version 3.0): https://scratch.mit.edu/projects/979822351/
hi875230163394: You're similar to valve in that you both hate a certain number…
Scratch 0.x, 1.x, 2.x, 3.x and LogoBlocks Archives
Bamboozlement: https://scratch.mit.edu/studios/33739789
Fun_Cupcake_i81: https://scratch.mit.edu/projects/850535211/
Years on internet: 15 (soon 16)
medians: Oh god not this utc - 12 thing again..
Fun_Cupcake_i81: What, were you expecting not to see the utc - 12 thing again? THE UTC - 12 THIGN ALWAYS RETURNS. ALWAYS.
medians: I knew it would happen. I was the one who started it last year.
Fun_Cupcake_i81: Well then if you didn't want it back maybe you need to time travel to last year and fix that

Oh wait if you could time travel I think we all know exactly when you would go-
user1: That picture is from 2.0. Now he’s at my house and is my pet.
user2: But this is medians we're talking about, so “from 2.0” can mean the same thing as “from five seconds ago”.

Detect Scratch version here
My other accounts: @selfexplanatory @modesties @chaircard @fireflyhero @dividendyield @colloids @radians @skeuamorphism @dihectogon @anglebisector @aau- @EditBlockColors @AdamantOrb @MoongeistBeam @festively @Ampharos_ @ straightforwardness
i trolled redcat LOL





if you see this
{what method did you use::control hat
answer on profile ::motion
} ::operators
;
WCBurns
Scratcher
15 posts

How do I code health?

Epic_Blue_Cat6412 wrote:

mra26b25c2 wrote:

Catscratcher07 wrote:

When I try to make it point to the player (the characters are simple cubes with hats) it just makes it go everywhere.
I'm also trying to make knockback so the enemy doesn't stay in one place when hit, as well as trying to code it so that the enemy moves toward the player, and when it dies it clones itself.

Maybe you could just do this:

when green flag clicked
set rotation style [left-right v]
Epic_Blue_Cat6412
Scratcher
36 posts

How do I code health?

@medians Could we collab?

Last edited by Epic_Blue_Cat6412 (Jan. 4, 2024 21:03:33)

Powered by DjangoBB