我在黄点(image)“点击”时有时会看到这个错误(有时会出现,有时不会出现),但我不明白为什么...... 我知道也许它看起来非常混乱,但这是我第一次使用java制作游戏。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 22
at Gameplay.paint(Gameplay.java:140)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这是我的绘画方法:
if (enemyXPos[xPos] == snakeXLenght[0] && enemyYPos[yPos] == snakeYLenght[0]) {
score++;
lenghtOfSnake++;
xPos = random.nextInt(34);
yPos = random.nextInt(23);
}
//line140:
enemyImage.paintIcon(this, graphics, enemyXPos[xPos], enemyYPos[yPos]);
for (int b = 1; b < lenghtOfSnake; b++) {
if (snakeXLenght[b] == snakeXLenght[0] && snakeYLenght[b] == snakeYLenght[0]) {
right = false;
left = false;
up = false;
down = false;
graphics.setColor(Color.white);
graphics.setFont(new Font("arial", Font.BOLD, 50));
graphics.drawString("YOU LOSE", 300, 300);
graphics.setFont(new Font("arial", Font.BOLD, 20));
graphics.drawString("Space to RESTART", 350, 340);
}
}
graphics.dispose();
}
这是我的阵列:
private int[] enemyXPos = {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[] enemyYPos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 575, 600, 625};
如果我没有显示足够的代码,只需说出来并粘贴更多。
答案 0 :(得分:0)
在Gameplay.java
,140
行(在您的paint()
方法中),您正试图从数组中获取内容。该数组小于您提供的索引(您尝试在索引22
处获取项目,但该数组可能包含21
个项目。
确保enemyXPos
和enemyYPos
分别等于或大于xPos
和yPos
。
答案 1 :(得分:-2)
此代码不足以给出具体答案,但看起来enemyXPos
或enemyYPos
尺寸不足。