Zombie game kill counter increments nonstop

Okay so I have been working on this project for quite a while. Basically I made a shooting game. Zombies are rendered on the screen frame by frame. and when they are shot, an explosion is rendered (Technically the zombie i object is still there but can’t be seen by the user) NO JOKE, been trying to do this for weeks:

I am trying to increment the kill counter each time I press on the zombie. The zombies spawning are directed into an ArrayList and they are clickable by the mousePressed operations.

The problem is, since they are in the draw() function, making a boolean that turns true when they are shot keeps incrementing the kill counter nonstop, or in other cases, keeps incrementing the kill counter as long as my mouse is clicked (since I’m human and I don’t click and release at the speed of a frame rate, the kill counter increases by 10±). This is my code to make the zombie clickable:

    if ( (mouseX >= zombieComp.x && mouseX <= zombieComp.x + 35) && (mouseY >= zombieComp.y && mouseY <= zombieComp.y + 50) && mousePressed && gameMode == PLAYING )
      zombieShot = true;
     incrementKills = true;

As you can see, first comes the dimensions of the zombie, and if my “mouse is pressed” at those dimensions while I am at the gameMode screen, the explosion is rendered and the zombie disappears (Again, he is technically there as an invisible object).

incrementKills is set to ‘false’ by default and this is its code:

if (incrementKills == true)
      incrementKills = false;

This really made me hate the draw function. Like how would you increment by just one, in any case, if the backbone of your whole program is the draw() function.

Again, I have been trying to do this for weeks. I have tried so many different ways but none of them work. It honestly is making me hate this project so much and I would appreciate it if you would help me. I can’t put the full code in here since it’s very long with a total of 6 different classes. It would confuse you a lot and I don’t want anyone to be stealing my work.


You should add a check to make sure that the zombie you shot is one that hasn’t been shot already (&& !zombieShot). If it hasn’t been shot but is now getting shot, record the hit immediately(killCounter++), and mark that zombie as shot. There’s no need to call other functions!