I’m trying to get this game to work. It’s not.
Iterator<Circle> iter = asteroids.iterator();
while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;
}
}
Iterator<Rectangle> iterB = bullets.iterator();
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 300 * Gdx.graphics.getDeltaTime();
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove();
}
/*if (Intersector.overlapCircleRectangle(asteroid, bullet)) {
// REMOVE OBJECT
}
*/
The part that isn’t working is the commented out section above. If you could tell me how to resolve that, it would be awesome. It won’t let me access asteroid or bullet unless I’m inside the while loop and I don’t know why.
UPDATE: Here is what I think you told me to try. It is still doing the same thing.
Iterator<Circle> iter = asteroids.iterator();
Iterator<Rectangle> iterB = bullets.iterator();
while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;
if (exploding) {
exploding = false;
explode.start();
}
}
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 300 * Gdx.graphics.getDeltaTime();
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove();
}
}
UPDATE: I’m getting an error occasionally on “iterB.remove();” and I don’t know why.
Code:
Iterator<Circle> iter = asteroids.iterator();
while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;
if (!exploding) {
exploding = true;
explode.start();
}
}
Iterator<Rectangle> iterB = bullets.iterator();
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 150 * Gdx.graphics.getDeltaTime();
if (Intersector.overlapCircleRectangle(asteroid, bullet)) {
iterB.remove();
iter.remove();
}
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove(); // ERROR HERE
}
}
Here’s the error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ArrayIndexOutOfBoundsException: -1
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at com.badlogic.gdx.utils.Array.removeIndex(Array.java:216)
at com.badlogic.gdx.utils.Array$ArrayIterator.remove(Array.java:433)
at com.chanceleachman.space.Screens.GameScreen.render(GameScreen.java:140)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.chanceleachman.space.Space.render(Space.java:37)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:190)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)
The
asteroidandbulletvariables are scoped to the while loop they are each defined in, and so cannot be used outside those scopes. You should review Java variable scoping.You could fix the compiler error by hoisting the declaration of
bulletandasteroidout of the while loops (i.e.,Circle asteroid; Rectangle bullet;), but I don’t think the resulting code would do what you intend (and it would crash where there are no bullets or no asteroids).You need to consider the case where there are no bullets (or no asteroids), and you need to consider the case where there are multiple bullets and multiple asteroids. You need to check every bullet against every asteroid to see if they intersect.