Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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 Projectiles  (Read 1779 times)
0 Members and 1 Guest are viewing this topic.
Offline Red_dino

Junior Newbie





« Posted 2013-03-24 19:43:49 »

I'm making a game kind of like Super Smash Bros. One of the attacks available to the player is a projectile fireball. All the fireball animation and collision is working perfectly, but whenever the code goes to remove a projectile from the screen, I get the following exception:
1  
2  
3  
4  
5  
6  
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
        at java.util.AbstractList$Itr.next(AbstractList.java:343)
        at Main.run(Main.java:213)
        at Main.<init>(Main.java:67)
        at Main.main(Main.java:307)

I'm using the following code for looping and removing:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 
//looping, this is inside the class that draws everything
for(Attack a: projectiles)
{
      a.update(bg);
}
//removing this is inside the projectile class
public void removeFromWorld()
{
    Main.projectiles.remove(this);
}


Currently I'm using an ArrayList to hold all the projectile data. Would it help if I used a different type of array or made a flag in the projectile class that indicated that the projectile should no longer be updated/drawn?

Thanks for the help!
Offline Agro
« Reply #1 - Posted 2013-03-24 22:34:42 »

In your base entity class, keep a boolean named expired. This will be set to false in the beginning. When the entity needs to be removed, set expired to true. In your update method or whatever, check if expired is true, and if it is, remove that entity.

Offline Red_dino

Junior Newbie





« Reply #2 - Posted 2013-03-25 00:03:45 »

The concurrent modification exception still happens.  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-03-25 00:06:17 »

Where is the remove being called, and how are you detecting when to remove which projectile?
Offline Red_dino

Junior Newbie





« Reply #4 - Posted 2013-03-25 00:28:16 »

Originally the remove was being called from inside the projectiles instance, but then I changed it using the flag method suggest by Agro. That didn't work either. The two instances in which the projectile is removed from the screen is when it leaves the viewing screen and when it hits the player or the AI (depending on who originally shot it). The collision detection is in the loop that I showed in the original post. I'm using
1  
projectiles.remove(a);
to remove the projectile.
Offline Longor1996
« Reply #5 - Posted 2013-03-25 00:35:15 »

Use the "if-alive-copy-into-new-list" approach.

Pseudo-Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
List newList = new List;

for(Entity e : currentList){
    e.logic();
   
    if(e.isAlive())
        newList.add(e);
}

currentList = newList;



- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline Red_dino

Junior Newbie





« Reply #6 - Posted 2013-03-25 00:43:48 »

That causes the game to crash the when the projectile is added rather than when it is removed. :/

Edit: I switched using straight up Arrays and Longor's method and now it works! Using an Array also drastically increased performance, now my tick timer is all messed up. Thanks to everyone who helped!
Offline cubemaster21
« Reply #7 - Posted 2013-03-25 11:49:32 »

It should probably look something like this.
1  
2  
3  
4  
5  
6  
7  
8  
for(int i = 0;i < projectiles.size();i++){
     Attack a = projectiles.get(i);
     if(a.exists){
          a.update(bg);
     } else {
          projectiles.remove(i);
     }
}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline davidc

Senior Member


Medals: 5
Projects: 2



« Reply #8 - Posted 2013-03-25 11:58:14 »

This is where Iterators still come in handy, as they are designed to allow removal of an element during traversal.

1  
2  
3  
4  
5  
6  
7  
8  
for (Iterator<Attack> i = projectiles.iterator(); i.hasNext();) {
    Attack a = i.next();
    if (a.exists() {
        a.update(bg);
    } else {
        i.remove(); // this will remove the current element from the iterator
   }
}


The code isn't as nice but they do the job.
Offline davedes
« Reply #9 - Posted 2013-03-25 17:26:46 »

Iterator is one solution; although some might argue it creates garbage within a frame loop. Probably negligible, but here are some other solutions for removing items from a list while iterating through it:

1  
2  
3  
4  
5  
6  
7  
for (int i=0; i<list.size(); i++) {
    list.get(i).update();
    if (!list.get(i).isActive()) { //entity is dead, remove
       list.remove(i);
        i--; //move back one
   }
}


The problem with the above is that it leads to a lot of shifting and arraycopies (moves all subsequent elements to the left by one) which can be slow. In reality the performance loss is negligible for most games, but here is another solution that doesn't lead to so much shifting:

1  
2  
3  
4  
5  
6  
for (int i=list.size()-1; i>=0; i--) {
    list.get(i).update();
    if (!list.get(i).isActive()) { //entity is dead, remove
       list.remove(i);
    }
}


The above introduces another problem; that you are iterating through in the reverse order. For many games this can be an issue, since generally you expect entities to be updated in the order they were created in.

A very clean and efficient solution is to actually to use a double-buffering technique, described here by Cas:
http://www.java-gaming.org/topics/arraylist-hashtable-or-for-holding-in-game-objects/25388/msg/219160/view.html#msg219160

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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (62 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!