Java 我得到;IndexOutOfBounds“;在我的彗星对宇宙飞船游戏中

Java 我得到;IndexOutOfBounds“;在我的彗星对宇宙飞船游戏中,java,user-interface,arraylist,indexoutofboundsexception,Java,User Interface,Arraylist,Indexoutofboundsexception,上周我开始开发一款“宇宙飞船vs彗星”风格的游戏,现在我已经停下来了 游戏的目的是在彗星经过你的飞船之前将其射出。你向彗星开火,使它们爆炸。简单的想法 然而,有时当我玩游戏时,我会出现“IndexOutOfBounds”错误。当我有一段时间没有开火时(我的射击ArrayList的大小为0),它几乎总是出现,然后当我开火,它碰撞时,它就会崩溃 所以我的代码中有一些错误,但我真的看不出来。现在,我希望你们中的一位能够明白为什么会出现这种情况,并使我免于进一步的“IndexOutOfBounds”错误

上周我开始开发一款“宇宙飞船vs彗星”风格的游戏,现在我已经停下来了

游戏的目的是在彗星经过你的飞船之前将其射出。你向彗星开火,使它们爆炸。简单的想法

然而,有时当我玩游戏时,我会出现“IndexOutOfBounds”错误。当我有一段时间没有开火时(我的射击ArrayList的大小为0),它几乎总是出现,然后当我开火,它碰撞时,它就会崩溃

所以我的代码中有一些错误,但我真的看不出来。现在,我希望你们中的一位能够明白为什么会出现这种情况,并使我免于进一步的“IndexOutOfBounds”错误:)

下面是失败的代码部分,包括我用来移动彗星和快照的函数:

游戏类

    if(!Game.player.getShots().isEmpty() && !comet.getComets().isEmpty()) { //Om de är tomma så ignorera

        for(int x = 0; x < Game.player.getShots().size(); x++) {    //Shots X

            if(!comet.getComets().isEmpty() && !comet.getComets().isEmpty()) {



                for(int y = 0; y < comet.getComets().size(); y++) {     //Comets Y

                    if(comet.getComets().get(y).intersects(Game.player.getShots().get(x)) && !comet.getComets().isEmpty() && !Game.player.getShots().isEmpty()) {   
    //the for loop above is the line that won't compile sometimes

                        comet.getComets().remove(y);
                        Game.player.getShots().remove(x);   

                        score++;
                    }

                }
            }

        }
    }

    //Comet spawn timer
    comet.addComets();

    //Move the comets and shots!
    Game.player.moveShots();
    comet.moveComets();
    repaint();
public ArrayList<Rectangle> getComets() {

    return comets;
}

public void moveComets() {
    if(!comets.isEmpty()) {

        for(int x = 0; x < comets.size(); x++) {

            comets.get(x).x -= cometSpeed;
        }
    }

}
if(!Game.player.getShots().isEmpty()&&!comet.getComets().isEmpty()){//Om deär tomma såignorea
对于(intx=0;x
彗星类

    if(!Game.player.getShots().isEmpty() && !comet.getComets().isEmpty()) { //Om de är tomma så ignorera

        for(int x = 0; x < Game.player.getShots().size(); x++) {    //Shots X

            if(!comet.getComets().isEmpty() && !comet.getComets().isEmpty()) {



                for(int y = 0; y < comet.getComets().size(); y++) {     //Comets Y

                    if(comet.getComets().get(y).intersects(Game.player.getShots().get(x)) && !comet.getComets().isEmpty() && !Game.player.getShots().isEmpty()) {   
    //the for loop above is the line that won't compile sometimes

                        comet.getComets().remove(y);
                        Game.player.getShots().remove(x);   

                        score++;
                    }

                }
            }

        }
    }

    //Comet spawn timer
    comet.addComets();

    //Move the comets and shots!
    Game.player.moveShots();
    comet.moveComets();
    repaint();
public ArrayList<Rectangle> getComets() {

    return comets;
}

public void moveComets() {
    if(!comets.isEmpty()) {

        for(int x = 0; x < comets.size(); x++) {

            comets.get(x).x -= cometSpeed;
        }
    }

}
public ArrayList getComets(){
返回彗星;
}
公共空间移动彗星(){
如果(!comets.isEmpty()){
对于(int x=0;x
玩家等级(本等级中有射击)

public void fire(){
添加(新矩形(x+player.width,y+23,shotWidth,shotHeight));
}
公共阵列列表getShots(){
回击;
}
公共空间{
如果(!shots.isEmpty()){
对于(int x=0;x
请记住,彗星和快照都是“对象矩形外的阵列列表”

我将在下面提供错误截图和游戏图片

上面的代码中标记了错误行,if语句应该阻止它崩溃(我想)


提前谢谢!感谢您的帮助!:)

如果(!comet.getComets().isEmpty()&&!comet.getComets().isEmpty()),请尝试在此处调整
。您正在检查comet阵列两次


如果(!comet.getComets().isEmpty()&&!comet.getComets().isEmpty()),请尝试在此处调整
。您正在检查comet阵列两次


让我知道它是否有效。

您应该更改if语句的顺序,以避免对其中一部分求值。 您应该将您的条件更改为:

if(x

这是因为您正在移除一个快照,并且在彗星内部,因为当在下一次彗星迭代中移除快照时,它将抛出
IndexOutOfBounds
,因为阵列不再具有您在if检查的快照,所以您需要再次检查快照上存在的x。 您也可以在for处执行此操作,检查这两个条件,然后让intersect作为唯一在if处进行检查

更好的性能是:

if(!Game.player.getShots().isEmpty() || !comet.getComets().isEmpty()) { 
//if one of them is empty, won't be intersections

        for(int x = 0; x < Game.player.getShots().size(); x++) {    //Shots X
                for(int y = 0; y < comet.getComets().size() && x < Game.player.getShots().size(); y++) {
 //Comets Y only if the shoot still available

                    if(comet.getComets().get(y).intersects(Game.player.getShots().get(x))) {   
    //the for loop above is the line that won't compile sometimes

                        comet.getComets().remove(y);
                        Game.player.getShots().remove(x);   

                        score++;
                        y = 0; // if you don't set the y = 0 the next shoot (as you removed the x, getShots.get(x) would be the x + 1 shoot) will only evaluate for the comets after y, won't evaluate the firsts comets at the array.
                    }

                }
            }

        }
如果(!Game.player.getShots().isEmpty()| | |!comet.getComets().isEmpty()){
//如果其中一个为空,则不会出现交叉点
对于(intx=0;x
您应该更改if语句的顺序,以避免它计算其中的一部分。 您应该将您的条件更改为:

if(x

这是因为您正在移除一个快照,并且在彗星内部,因为当在下一次彗星迭代中移除快照时,它将抛出
IndexOutOfBounds
,因为阵列不再具有您在if检查的快照,所以您需要再次检查快照上存在的x。 您也可以在for处执行此操作,检查这两个条件,然后让intersect作为唯一在if处进行检查

更好的性能是:

if(!Game.player.getShots().isEmpty() || !comet.getComets().isEmpty()) { 
//if one of them is empty, won't be intersections

        for(int x = 0; x < Game.player.getShots().size(); x++) {    //Shots X
                for(int y = 0; y < comet.getComets().size() && x < Game.player.getShots().size(); y++) {
 //Comets Y only if the shoot still available

                    if(comet.getComets().get(y).intersects(Game.player.getShots().get(x))) {   
    //the for loop above is the line that won't compile sometimes

                        comet.getComets().remove(y);
                        Game.player.getShots().remove(x);   

                        score++;
                        y = 0; // if you don't set the y = 0 the next shoot (as you removed the x, getShots.get(x) would be the x + 1 shoot) will only evaluate for the comets after y, won't evaluate the firsts comets at the array.
                    }

                }
            }

        }
如果(!Game.player.getShots().isEmpty()| | |!comet.getComets().isEmpty()){
//如果其中一个为空,则不会出现交叉点
对于(intx=0;x