我正在嘗試制作蛇游戲。我已經(jīng)快完成了。問題是,有時(shí)當(dāng)蛇吃掉敵人的物體時(shí),會(huì)彈出此錯(cuò)誤--->Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 33er.java:814)這是敵人的位置單元--->private int[] enemyYpos = {25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,525,550,575,600,625,650,675,700,725,750,775,800,825,850};private int[] enemyXpos = {100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,525,550,575,600,625};這是隨機(jī)數(shù)--->private int xpos = r.nextInt(34);private int ypos = r.nextInt(20);現(xiàn)在,我通過使用for循環(huán)來繪制敵人,在其中定義了敵人的位置-> enemy = new ImageIcon("E:\\Netbeans\\old files\\Game\\src\\game\\bug.png"); if((enemyXpos[xpos] == snakeXlength[0] && enemyYpos[ypos] == snakeYlength[0])){ lengthsnake++; xpos = r.nextInt(34); ypos = r.nextInt(20); } enemy.paintIcon(this, g, enemyXpos[xpos], enemyYpos[ypos]); //The Exception error shows that the mistake is in this line我希望這些信息足夠了。提前致謝!:)
2 回答

狐的傳說
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
您的問題是您混合使用X和Y。它應(yīng)該是
xpos = r.nextInt(20);
ypos = r.nextInt(34);
而不是相反。
添加回答
舉報(bào)
0/150
提交
取消