Results 1 to 2 of 2
  1. #1
    djokovic is offline Member
    Join Date
    May 2012
    Rep Power

    Default Need help with array reading

    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:

    Java Code:
    	Input in = gc.getInput();
    // Check every array element
    				for(int a=0; a<=counter; a++){
    					if(zombies[a] != null){
    						// Increasing zombie
    						// 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()))){
    								System.out.println("Zombie "+a);
    								if(currentWeapon.getAmmo() > 0){
    							if(zombies[a].isAlive() == false){
    								zombies[a] = null;
    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)?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Need help with array reading

    Moved from Advanced Java.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Help with GUI, Array, and Reading File
    By bamagirl31 in forum New To Java
    Replies: 21
    Last Post: 07-05-2011, 01:14 AM
  3. Help with reading file into array
    By xkillswitchx14 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 10:24 PM
  4. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 08:36 AM
  5. Array not reading. Plz help
    By cmizer in forum New To Java
    Replies: 14
    Last Post: 02-12-2009, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts