This site requires JavaScript, please enable it in your browser!
Greenfoot back
charliejustshrekedyou
charliejustshrekedyou wrote ...

2019/3/23

Need help with a health counter...

In my game my actor loses a life when hit by an opposing laser. This works, however a life is lost for every single pixel of my character that the laser passes through, resulting in my lives count reaching -200 in a single hit. How do I fix this? Here's the relevant code for my Laser: if (isTouching(Hakim.class)) { World myWorld = getWorld(); MyWorld myworld = (MyWorld)myWorld; Counter counter = myworld.getCounter(); counter.loseHealth(-1); } And here's the relevant code for my counter: public void loseHealth(int amount) { health += amount; setImage(new GreenfootImage("Lives : " + health, 24, Color.BLUE, Color.BLACK)); }
Super_Hippo Super_Hippo

2019/3/23

#
You could remove the Laser when it touches a character. Or you have a boolean which tracks if it is touching it and it is only dealing damage when it changes from "not touching" to "touching".
That worked!!! Thank you!!!
You need to login to post a reply.