开发者社区> 问答> 正文

我在彗星和太空飞船游戏中得到“ IndexOutOfBounds”

我上周开始开发“太空飞船与彗星”风格的游戏,现在我停了下来。

游戏的目的是在彗星通过您的飞船之前对其进行射击。您通过向它们射击使彗星爆炸。简单的想法!

但是,有时在我玩游戏时会出现“ 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;
        }
    }

}

播放器类(此类射击)

public void fire() {


    shots.add(new Rectangle(x + player.width, y + 23, shotWidth,shotHeight));
}
public ArrayList<Rectangle> getShots() {

    return shots;

}

public void moveShots() {
    if(!shots.isEmpty()) {
        for(int x = 0; x < shots.size(); x++) {

            shots.get(x).x += fireSpeed;
        }
    }
}

请记住,彗星和镜头都是“矩形对象之外的ArrayList”

错误行在上面的代码中标记,if语句应阻止它崩溃(我认为)。

提前致谢!感谢所有帮助!:)

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 09:05:40 366 0
1 条回答
写回答
取消 提交回答
  • 您应该在if语句中更改顺序,以免对其一部分进行求值。您应该将条件更改为:

    if( x < Game.player.getShots().size() && comet.getComets().get(y).intersects(Game.player.getShots().get(x))) {

    那是因为您要删除镜头,并且在彗星内部,因为在下一次彗星迭代时删除镜头时,它将被抛出,IndexOutOfBounds因为数组不再具有您在if处检查的镜头,因此您需要再次检查用于拍摄中的x。您也可以在处进行检查,同时检查两个条件,然后让相交处仅在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.
                        }
    
                    }
                }
    
            }
    

    回答来源:Stack Overflow

    2020-03-27 09:06:11
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
工业互联网为智能机床插上翅膀 立即下载
运动手环是伪需求 智能装备才是“王道” 立即下载
天合光能-用计算 捉“光的能量” 立即下载