使用Java编写游戏时的IndexOutOfBoundsError

时间:2013-04-03 13:37:29

标签: java indexoutofboundsexception

我在编写学校项目时遇到了IndexOutOfBound错误。这个问题不稳定,这个问题似乎很随机发生......

public void run(){ // Main game methode 
    Image Background = this.images.get(0);//get background because we can
    Player Player1 = this.players.get(0);//get player 1
    Player Player2 = this.players.get(1);//get player 2

    Player1.setHorSp(1);//give start speed to player 1
    Player2.setHorSp(1);//give start speed to player 2
    while(this.running == true && this.crash == false){ // main game loop, loops when game is started and there is no crash
        // Player 1
        if(this.turnable == 0 && Player1.getDir() > 0){
            Player1.setPrevLine(Player1.getCurrentLine());//safe previous line for testing
            if(Player1.getDir() == 1){ //check which key and horizontal speed so it can't move in opposite direction
                if(Player1.getHorSp() == -1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX());
                    Player1.setY(Player1.getY()-Player1.getWidth()+Player1.getHeight());
                }
                if(Player1.getHorSp() == 1){
                    Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                    Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                }
                Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                Player1.setImage("BikeBlueUp.png"); //change image in the right direction
                Player1.setWidth(grid); //correct images size
                Player1.setHeight(2*grid);
                Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                Player1.setLineY(Player1.getY()+grid/2);
                Player1.setHorSp(0); //give speed in the right direction
                Player1.setVerSp(-1);
            }
            if(Player1.getDir() == 2){ //check which key and if is in grid
                if(Player1.getVerSp() == -1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX());
                    Player1.setY(Player1.getY());
                }
                if(Player1.getVerSp() == 1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX());
                    Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                }
                Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                Player1.setImage("BikeBlueRight.png"); //change image in the right direction
                Player1.setWidth(2*grid); //correct images size
                Player1.setHeight(grid);
                Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2);//set point line is atteched to bibe
                Player1.setLineY(Player1.getY()+grid/2);
                Player1.setHorSp(1); //give speed in the right direction
                Player1.setVerSp(0);
            }
            if(Player1.getDir() == 3){ //check which key and horizontal speed so it can't move in opposite direction
                if(Player1.getHorSp() == -1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX());
                    Player1.setY(Player1.getY());
                }
                if(Player1.getHorSp() == 1){
                    Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                    Player1.setY(Player1.getY());
                }
                Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                Player1.setImage("BikeBlueDown.png"); //change image in the right direction
                Player1.setWidth(grid); //correct images size
                Player1.setHeight(2*grid);
                Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                Player1.setHorSp(0); //give speed in the right direction
                Player1.setVerSp(1);
            }
            if(Player1.getDir() == 4){ //check which key and horizontal speed so it can't move in opposite direction
                if(Player1.getVerSp() == -1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                    Player1.setY(Player1.getY());
                }
                if(Player1.getVerSp() == 1){
                    Player1.setLineX(Player1.getX()+grid/2); //set end of line
                    Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                    addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                    Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                    Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                    Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                }
                Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                Player1.setImage("BikeBlueLeft.png"); //change image in the right direction
                Player1.setWidth(2*grid); //correct images size
                Player1.setHeight(grid);
                Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                Player1.setLineY(Player1.getY()+grid/2);
                Player1.setHorSp(-1); //give speed in the right direction
                Player1.setVerSp(0);
            }
        }

这会产生错误:

Exception in thread "Thread-5" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Game.run(Game.java:388)
at java.lang.Thread.run(Thread.java:619)

按下向上按钮时,现在仍然出现此错误。其他2次没有发生此错误,现在已经发生了。

按下右键后出现此错误

Exception in thread "Thread-5" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Game.run(Game.java:388)
at java.lang.Thread.run(Thread.java:619)

按下右键后产生了这个错误,它在同一个游戏中工作了1次......即使经过3天的调试,我们仍然看不到赞助人。

以前有没有人见过这个,如果有的话:我们该如何解决?

- JYT

聚苯乙烯。整个代码在这里http://pastebin.com/0wvyr3qh

1 个答案:

答案 0 :(得分:0)

我不确定您尝试做什么,但错误发生的原因是: Player2.getCurrentLine()返回this.lines.size()的值, 因此,您尝试访问列表中的不存在的对象(如果列表大小为5,对象为0-4)

如果你的网格是循环的,你可以使用:

Line CurrentLineP2 = this.lines.get(Player2.getCurrentLine() % this.lines.size());

而不是:

Line CurrentLineP2 = this.lines.get(Player2.getCurrentLine());

如果它不是循环的,你需要处理这种情况。玩家如何处于不存在的界限?

无论如何,添加所有相关代码将有助于我们为您提供帮助。