Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting array of all surrounding mobs  (Read 508 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727

Senior Member


Projects: 2



« Posted 2013-02-03 01:49:34 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   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 Tongue. Thanks for any help, cMp
Online Magn919

Senior Newbie


Medals: 1



« Reply #1 - Posted 2013-02-03 01:57:50 »

Try replacing that return statement with a break statement.

For every new problem, a new source of solutions has come to exist.
Offline Slyth2727

Senior Member


Projects: 2



« Reply #2 - Posted 2013-02-03 02:01:58 »

Does the exact same thing. This method gets called every frame update so that's why neither work.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline jonjava

JGO Knight


Medals: 32



« Reply #3 - Posted 2013-02-03 02:06:06 »

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

Offline Slyth2727

Senior Member


Projects: 2



« Reply #4 - Posted 2013-02-03 02:09:46 »

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
Offline jonjava

JGO Knight


Medals: 32



« Reply #5 - Posted 2013-02-03 02:16:39 »

how about something like

1  
2  
3  
4  
5  
6  
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?

Offline Slyth2727

Senior Member


Projects: 2



« Reply #6 - Posted 2013-02-03 02:28:16 »

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?
Offline Slyth2727

Senior Member


Projects: 2



« Reply #7 - Posted 2013-02-03 02:29:43 »

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

JGO Knight


Medals: 32



« Reply #8 - Posted 2013-02-03 02:41:20 »

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.

1  
2  
3  
4  
5  
6  
7  
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:

1  
2  
3  
4  
5  
6  
7  
8  
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):

1  
2  
3  
4  
// 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

1  
2  
3  
4  
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");
}

Offline Slyth2727

Senior Member


Projects: 2



« Reply #9 - Posted 2013-02-03 02:53:17 »

Perfect, thank you very much I cannot BELIEVE that I didnt think of an ArrayList!  These are the best forums in town!
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (88 views)
2013-05-16 21:24:48

gouessej (117 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.156 seconds with 21 queries.