How do you just add a number once when something is triggered.
I was making a boss fight and I want to know how this would work. My code goes as followed
if distance(0,30,boom.x,boom.y) < 30 then
health1 = health1 - 1
end
I want to trigger it once, and not multiple times. This is once again in Wreck Of The Seas.
https://microstudio.dev/i/JustNitro/wreckoftheseas/
if distance(0,30,boom.x,boom.y) < 30 and damaged == false then
health1 = health - 1
damaged = true
else
damaged = false
end
This is a trashy way of doing it, but you should get the general idea. We will just use a bool to check if we already got the damage we needed. If we did then it is true if we didn't it is false so now it will only subtract it once. But this is all in theory.
Didn't work when I plugged everything in :\
I modified it and it works decently, thanks!
The most common solution is to add "iframes" (or invincibility frames). Basically, you say something like
if distance(0,30,boom.x,boom.y) < 30 and invincibility <= 0 then
health = health - 1
invincibility = 30
end
(then somewhere else in your update loop)
invincibility = invincibility - 1
This code makes the enemy "invincible" for a bit because the damage function won't activate if it is. The reason it's 30 is so you have half a second before invincibility is 0 or less again. You can change the 30 to whatever you like, just remember that 60 = 1 second. if you have anymore questions, just ask :)
P.S. this is essentially the same as MrBoi's method, however with this one you can change the amount of time the enemy is invincible for. Also, you probably will want it as a property of an object, like boss.invincibility, so that in the future you can have multiple bosses without worries of problems arising from them all sharing the same variable for invincibility.
Exactly as JmeJuniper said!
One small thing, when you are writing code in posts you can use triple backquotes (these things: `) to put it into a special 'box' and make it look better.
Like this:
if distance(0,30,boom.x,boom.y) < 30 and invincibility <= 0 then
health -= 1
invincibility = 30
end
(then somewhere else in your update loop) invincibility = invincibility - 1
See more info on @gilles markdown post
https://microstudio.dev/community/tips/use-markdown-in-forum-posts-and-replies/26/
It isn't really necessary, but I think it looks nice :-)
@this_name_is_taken Ohh yeah thank you I forgot for a bit that you could use markdown XD I'm just gonna edit my post real quick...
@JmeJuniper Looking good ;)