Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Removing bullet object from ArrayList error.  (Read 2699 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2012-04-20 21:08:37 »

Hey, I get random(!) errors while removing my bullet from ArrayList once it hits the player.
I think its because of the for loop but dont know how to do it different...

This is how I create the bullet:
1  
    ArrayList <Projectile> enemyBullets = new ArrayList<Projectile>();  // GegnerTyp2 Geschosse


1  
2  
3  
  
projectile = new Projectile(enemyContainer.get(enemys).x, enemyContainer.get(enemys).y, sprites.projectile ,2);
enemyBullets.add(projectile);


This is the collision:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
 // Go through each enemy bullets
       for(int x = 0; x < enemyBullets.size(); x++)
       {
           
        // Go through each enemy
               for(int z = 0; z < enemyContainer.size(); z++)
               {
                   // Does the bullet hit the player?
                    if(enemyBullets.get(x).x > player.x && enemyBullets.get(x).x < player.x + 64 + value
                    && enemyBullets.get(x).y > player.y && enemyBullets.get(x).y < player.y + 37 )
                    {
                        // If the player has shield left it will damage this first.
                        if(player.shield>0)
                             player.shield = player.shield - 2f;
                        else
                        {
                            // The damage the player get
                            damageValue = (enemyContainer.get(z).laser - player.armor);
                           
                            if(damageValue>0)
                              player.hp = player.hp - Math.abs(damageValue);
                        }
                        enemyBullets.get(x).sprite = sprites.emptyProjectile; // I remove the bullet graphic here.
                        enemyBullets.remove(x);   //!!! Here I remove the bullet but I get random error.
                    }
                }
             
       }


So any ideas how I can solve it to remove the bullet without this error warning:
Quote
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 22, Size: 22
   at java.util.ArrayList.RangeCheck(ArrayList.java:547)
   at java.util.ArrayList.get(ArrayList.java:322)
Offline princec

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2012-04-20 21:26:12 »

You don't break out of the inner loop having removed a bullet. Which means the same bullet might hit two gidrahs and you then attempt to remove it again.

Cas Smiley

Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #2 - Posted 2012-04-20 21:33:06 »

Hm...But I have to remove the bullet when it hits the player...
So there do you say I should place it?

And what is an gidrahs?? The Player? The bullet only can hit one event and thats the player. Hm..

(Ah and I forgot to take out the for loop with the enemyContainer, I dont need anymore)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deathpat
« Reply #3 - Posted 2012-04-20 21:42:29 »

if you remove the enemyContainer loop, you just have to decrease x after the removal so that you don't skip a bullet, but anyway in this case you shouldn't get an IndexOutOfBoundsException anymore.
I suggest you to use an Iterator to loop through the bullets and use its remove method to remove the current bullet from the list ... this way you will not have to handle the index yourself. And instead of doing lots of enemyBullets.get(x), do it once at the begining of the loop and store it in a variable ( if you use an Iterator you will be forced to anyway Smiley ).

work in progress : D A E D A L U S
Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-04-21 02:08:14 »

A gidrah is princec's fancy shmancy way of calling enemies Tongue

If 'x' is the last element in the list, removing it makes 'x' outside the bounds of the list. Since you don't break out of the inner loop, you are accessing 'x' again! What you should do is stop the inner loop as soon as the bullet is removed, by using a boolean or something.

Offline roland
« Reply #5 - Posted 2012-04-21 03:22:59 »

You shouldn't remove bullets while looping through them either, it will cause a concurrent modification exception.

Either make a seperate list of bullets to remove, which you then remove all at once after the collision code with enemyBullets.removeAll(bulletsToRemove);

or use an iterator(better) as deathpat said:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
Iterator<Projectile> it = enemyBullets.iterator();

while (it.hasNext())
{
      Projectile bullet = it.next();
      boolean hit = false;
      for(Enemy enemy: enemyContainer)
      {
             //do collision
             <if collided> //TODO replace this line with your collision code
             hit = true;
             break;
      }
      if (hit)
         it.remove(); //removes the current bullet from the list of bullets SAFELY.
}



Your inner loop also does some strange stuff.
You are checking if the bullet collides with the player for each enemy. You should only check if each bullet collides with the player once.

1  
2  
3  
4  
5  
6  
7  
for(int x = 0; x < enemyBullets.size(); x++)
       {
              //do player-bullet collision here

              //you should make each bullet store a damage variable, then you dont need to loop through the enemies at all.
              //when the enemy creates a bullet, it gives the bullet the amount of damage an enemy does.
}







Offline ReBirth
« Reply #6 - Posted 2012-04-21 03:40:13 »

Create another ArrayList to hold queue of being removed bullet. Then use removeAll(ArrayList) method to clear main ArrayList so you can avoid that issue.

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1569 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!