我正在努力让这款游戏正常运行。它不是。
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
}
*/
不工作的部分是上面注释掉的部分。如果你能告诉我如何解决这个问题,那就太棒了。它不会让我访问小行星或子弹,除非我在 while 循环中并且我不知道为什么。
更新:这是我认为你告诉我尝试的。它仍在做同样的事情。
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();
}
}
更新:我偶尔会在“iterB.remove();”上遇到错误 我不知道为什么。
代码:
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
}
}
这是错误:
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)