LibGDX中的碰撞

时间:2013-01-02 00:30:03

标签: java iterator libgdx

我正试图让这个游戏发挥作用。不是。

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)

1 个答案:

答案 0 :(得分:1)

asteroidbullet变量的范围限定为它们各自定义的while循环,因此不能在这些范围之外使用。您应该查看Java variable scoping

您可以通过从while循环(即bullet)中提升asteroidCircle asteroid; Rectangle bullet;的声明来修复编译器错误,但我不认为生成的代码会做你想做的事(如果没有子弹或没有小行星,它会崩溃。)

你需要考虑没有子弹(或没有小行星)的情况,你需要考虑有多个子弹和多个小行星的情况。你需要检查每个小行星对每个小行星,看看它们是否相交。

相关问题