Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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 1067 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-02-03 00: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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #1 - Posted 2013-02-03 00: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
« Reply #2 - Posted 2013-02-03 01:01:58 »

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-02-03 01:06:06 »

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

Offline Slyth2727
« Reply #4 - Posted 2013-02-03 01: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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline jonjava
« Reply #5 - Posted 2013-02-03 01: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
« Reply #6 - Posted 2013-02-03 01: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?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #7 - Posted 2013-02-03 01:29:43 »

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline jonjava
« Reply #8 - Posted 2013-02-03 01: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
« Reply #9 - Posted 2013-02-03 01:53:17 »

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-21 02:42:18

BurntPizza (15 views)
2014-09-21 01:30:30

moogie (18 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (43 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (97 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!