Iterator<Asteroid> asteroidsIterator = asteroids.iterator();
while (asteroidsIterator.hasNext()) {
Asteroid asteroid = asteroidsIterator.next();
asteroid.x -= 200 * Gdx.graphics.getDeltaTime();
if (asteroid.x < 0) {
asteroidsIterator.remove();
} else if (asteroid.overlaps(ship)) {
//quitGame();
}
}
Iterator<Rectangle> lasersIterator = lasers.iterator();
asteroidsIterator = asteroids.iterator(); //<-- it seems that this does not get a fresh iterator
while (lasersIterator.hasNext()) {
Rectangle laser = lasersIterator.next();
laser.x += 600 * Gdx.graphics.getDeltaTime();
if (laser.x > 800) {
lasersIterator.remove();
Gdx.app.log("SC", "Laser exited screen.");
} else {
while (asteroidsIterator.hasNext()) {
Asteroid asteroid = asteroidsIterator.next();
if (asteroid.overlaps(laser)) {
Gdx.app.log("SC", "COLLISION");
asteroidsIterator.remove();
lasersIterator.remove();
}
}
}
}
When I do full-automatic fire then some asteroids are not hit. Only newly created asteroids after begining firing are destroyed and in addition to that they should be very much to the right of the screen.
EDIT:
After further investigation it seems that the second call to asteroids.iterator() does not create a fresh instance. Is there a way to get a fresh/reset Iterator?