Getting array of all surrounding mobs

Right, in my TD game, I am making a tesla tower. If you dont know, a tesla tower has small radius and every once and a while shoots out lightning in all directions harming every mob in radius. I am trying to get an array of all the mobs in radius so I can simply go through a for loop every time the tower “electrocutes” so I can harm all of them. Here is the code:

	public void getMobs() {
		for (int i = 0; i < Screen.mobs.length; i++) {
			if (Screen.mobs[i] != null && circle != null) {
				if (Screen.mobs[i].inGame) {
					if (circle.intersects(Screen.mobs[i])) {
						for (int s = 0; s < mobsIR.length; s++) {
							if (mobsIR[s] == null) {
								mobsIR[s] = Screen.mobs[i];
								System.out.println("TARGETED: " + Screen.mobs[i] + " AT INDEX: " + s);
								return;
							}
						}
					}
				}
			}
		}
	} 

The problem is that every time this is called it takes the first mob in radius and assigns it to every number in the the array “mobsIR[]”. I know it seems simple but I cant think of a way to do this :P. Thanks for any help, cMp

Try replacing that return statement with a break statement.

Does the exact same thing. This method gets called every frame update so that’s why neither work.

Why do you return or break at all? Try removing it.

Well when I had the break or return it sets the mob to every other index of the array. Without one its sets every index. Thats rather confusing I am not sure why it does that

how about something like

for ( int i = 0; i < Screen.mobs.length; i++ ) {
   Mob mob = Screen.mobs[i];
   if ( circle.intersects( mob ) ) {
      System.out.println("TARGET: " + mob.name + " in range!");
   }
}

I don’t really know why you have another for loop inside there?

Ah that works fine!! Now I just need a way to set the index that mob was assigned to to null when it is out of range?

I also had another for loop because Screen.mobs is bigger than mobsIR

Well if you only have 1 or very few Tesla’s and not too many objects you can simply do what needs to be done inside the loop itself.

for ( int i = 0; i < Screen.mobs.length; i++ ) {
   Mob mob = Screen.mobs[i];
   if ( circle.intersects( mob ) ) {
      System.out.println("TARGET: " + mob.name + " in range!");
      mob.killObjectByLightning(); // or something
   }
}

Or if you want to save all the mobs that are in range into another list:

tesla.mobsList.clear(); // remove old mobs
for ( int i = 0; i < Screen.mobs.length; i++ ) {
   Mob mob = Screen.mobs[i];
   if ( circle.intersects( mob ) ) {
      System.out.println("TARGET: " + mob.name + " in range!");
      tesla.mobsList.add( mob ); // add mob into the list
   }
}

where tesla.mobsList is an ArrayList for example (instead of a constant size normal array):

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

then you could loop through the mobs inside the ArrayList like this (for example):

// this is called a 'for-each' loop, i.e, for each mob in mobsList do { this }
for ( Mob mob : tesla.mobsList ) {
   System.out.println(mob.name + " is in range of the tesla");
}

or

for ( int i = 0; i < tesla.mobsList.size(); i++ ) {
   Mob mob = tesla.mobsList.get( i );
   System.out.println(mob.name + " is in range of the tesla");
}

Perfect, thank you very much I cannot BELIEVE that I didnt think of an ArrayList! These are the best forums in town!