Hello, I'm creating 2D FPS game with Slick 2D game engine. Where user need to shoot zombies. I made, that zombies would appear every 5 seconds. But I have I bug. If there are more than one zombie currently on the screen, user, can shoot only that, who appeared in the screen earliest. I will make example :
5 seconds...
Zombie 1 appears
I shoot him
5 seconds...
Zombie 2 appears
5 seconds
Zombie 3 appears
In this situation, I can shoot only zombie 2(he is the earlieast), and zombie 3 is untouchable.
This is code, which checks if user hits zombie:
Code:
Input in = gc.getInput();
// Check every array element
for(int a=0; a<=counter; a++){
if(zombies[a] != null){
// Increasing zombie
zombies[a].setWidth(getSpeed()*delta);
zombies[a].setHeight(getSpeed()*delta);
// Checking if user hit zombie
if(((posX > zombies[a].getZombieX()) && (posX < zombies[a].getZombieX()+zombies[a].getWidth()))&&((posY > 600-zombies[a].getHeight()-zombies[a].getZombieY())&&(posY < 600-zombies[a].getZombieY()))){
if(in.isMousePressed(0)){
System.out.println(counter);
System.out.println("Zombie "+a);
if(currentWeapon.getAmmo() > 0){
zombies[a].shooted(currentWeapon.getDamage());
currentWeapon.shooted();
currentWeapon.shoot();
}else{
}
}
if(zombies[a].isAlive() == false){
zombies[a] = null;
countingDeads++;
addPoints(50);
}
}
}
I realised, that this loop only checks earlieast array element, which is not null. But I need to check every element. (Counter variable, which is used in loop header, increases every 5 seconds, until he reaches 9).
I added some println statements in the code, and realised, that my theory is true.
So, how I can fix this? (Make loop to check all existing elements not only earliest)?