Problems with ConcurrentModificationException
in
Programming Questions
•
1 year ago
Hey Folks,
So i recently dug out some old Games, and one of it was Operation: InnerSpace. Since it won't run on Windows 7 and lags horribly in VirualPC I tried to make something similar in Processing. But I've encountered a problem.
I'm storing Bullets and Enemies and that stuff in ArrayLists, and when they are removed from the list I alway get java.util.ConcurrentModificationException. I think it is because the functions in the draw loop try to access stuff that is currently being changed by the GameLogic thread, but I can't find a way to avoid it. Currently i just catch the exceptions, but that causes stuff to to disappear for one frame when the exception is thrown.
I already tried to put a boolean in the thread that goes true if the thread is working and stops the execution of the draw loop until it is finished, but it didn't help.
Could somebody please help me with this?
Draw Loop:
- void draw() {
- background(bg);
- void drawBullets() {
- try {
- for (Bullet b: bullets) {
- b.disp();
- }
- } catch(Exception e) {
- println("Bullet: " + e);
- }
- }
- ...
- // other stuff goes here...
- }
- void gameLogic() {
- ...
- // keyboard control stuff...
- ...
- ArrayList<Bullet> bulletsToDelete = new ArrayList<Bullet>();
- for (Bullet b: bullets) {
- b.go();
- if (millis() - b.start > b.lifetime) {
- bulletsToDelete.add(b);
- } else if (b.xpos <= myShip.xpos + myShip.xsize/2 && b.xpos >= myShip.xpos - myShip.xsize/2 && b.ypos <= myShip.ypos + myShip.ysize/2 && b.ypos >= myShip.ypos - myShip.ysize/2) {
- if (b.enemy) {
- b.hit();
- myShip.hit(b.damage);
- bulletsToDelete.add(b);
- }
- } else {
- for(Training_Target t: targets) {...
- ...
- // other game logic stuff...
- ...
- try {
- Thread.sleep(16);
- } catch (Exception e) {
- println("Sleep: " + e);
- }
- }
- }
1