0
\$\begingroup\$

I'm trying to make the player loose one heart then cool down for a second after being hit. For an example

if(a.overlaps(b)) {
p.health--;
}

would lower the health while being touched. In my old game I used a thread algorithm to wait a second, but that took a while. Besides doing some kind of complicated timer method, what can I do to keep to the player hit one at a time?

\$\endgroup\$
0
\$\begingroup\$

Give the player a number in their class called hitCountdown or something. Every time they are hit, set that number to the amount of time you want to give the player to escape. Then, where you update the player, decrement the countdown

if(hitCountdown > 0) hitCountdown--;

Now, when you hit the player and damage him, write this:

if(a.overlaps(b)) {
    //if it has been a while since we got hit
    if(hitCountdown <= 0){
        p.health--;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.