// array containing the active humans. public final Array<Human> activeHumans = new Array<Human>(); // object pool. public final Pool<Human> humanPool = new Pool<Human>() { @Override protected Human newObject() { return new Human(100, 500); } }; 

 @Override public void update(float dt) { checkCollisions(); } public void checkCollisions() { // human-human collision for (Human h1 : activeHumans) for (Human h2 : activeHumans) if (h1.getRectangle().overlaps(h2.getRectangle())) { h1.setX(h1.getX() + 2); h2.setX(h2.getX() - 2); } } 

Here he throws me out with Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: #iterator() cannot be used nested. . If you make human-otherObject or even

 for (Human h1 : activeHumans) for (Human h2 : activeHumansOther) 

, then everything works, but, of course, not with those that I want (only human - humanOther , and I need human - human ). How to make it so that the first option works - when the humanes collided, they diverged in different directions?

    1 answer 1

    The for loop in the for each style in Java uses an iterator to position the collection.

    The for each loop can be used for any objects that implement the Iterable<T> interface.

    For example, the cycle:

     for (Integer item: list) { System.out.println(item); } 

    equivalent to this cycle:

     for(Iterator<Integer> i = list.iterator(); i.hasNext(); ) { Integer item = i.next(); System.out.println(item); } 

    Due to the use of an iterator, you get an exception:

     ... GdxRuntimeException: #iterator() cannot be used nested. 

    As a solution to the problem, you can use the usual for loop:

     for (int i=0; i<activeHumans.size(); i++) { Human h1 = activeHumans.get(i); for (int j=0; j<activeHumans.size(); j++) { Human h2 = activeHumans.get(j); // необходимые действия } } 
    • Hmm, the next problem arose - he now considers h1 and h2 to be the same. <br/> for (int i=0; i<activeHumans.size(); i++) { Human h1 = activeHumans.get(i); for (int j=0; j<activeHumans.size(); j++) { Human h2 = activeHumans.get(j); if (h1 != h2) if (h1.getRectangle().overlaps(h2.getRectangle())) { h1.setX(h1.getX() + 2); } } for (int i=0; i<activeHumans.size(); i++) { Human h1 = activeHumans.get(i); for (int j=0; j<activeHumans.size(); j++) { Human h2 = activeHumans.get(j); if (h1 != h2) if (h1.getRectangle().overlaps(h2.getRectangle())) { h1.setX(h1.getX() + 2); } } for (int i=0; i<activeHumans.size(); i++) { Human h1 = activeHumans.get(i); for (int j=0; j<activeHumans.size(); j++) { Human h2 = activeHumans.get(j); if (h1 != h2) if (h1.getRectangle().overlaps(h2.getRectangle())) { h1.setX(h1.getX() + 2); } } and here, at the intersection of the figures, both move to getX() + 2 - Krem Soda
    • @Krem Soda, Try to compare not the objects, but the indices: if (i != j) . - post_zeew
    • Compared only indexes, compared 'activeHumans.get (i)! = ActiveHumans.get (j)', added after that '.get (i) .getRectangle ()' - the same. If you do not make this comparison, then when a human appears, it immediately starts moving, and when comparing, human begins to move only when it touches (also tried to do activeHumans.get (i) .getRectangle (). X + = 2 '- still moving both). The program does exactly what I say to her, which means that I tell her something wrong, but I cannot understand what it is - Krem Soda
    • That's what you had to do! for (int j=i+1; j<activeHumans.size(); j++) (addition to the accepted answer) - Krem Soda