我一直在网上学习制作2D游戏的教程,但希望能对其进行扩展。当我尝试在库存中移动物品时,一切都很顺利。我可以成功地提取物品并将其放回invBar,但这不允许我将其放进库存invBag。我已经注意到了他所做的一切,但由于某种原因,他没有犯错误。
我是一个初级程序员,到目前为止,我知道这个错误是由数组"越界"(正如错误的标题明显提到的那样)造成的,但我不明白它是如何"越界"的。
编辑:GAWD我是个白痴。。。出于某种原因,我一直在键入1而不是I,显然这就是问题所在。
库存代码:
package y.m.m //to protect project name
import java.awt.*;
import java.awt.event.*;
public class Inventory {
public static Cell[] invBar = new Cell[Tile.invLength];
public static Cell[] invBag = new Cell[Tile.invLength * Tile.invHeight];
public static boolean isOpen = false;
public static boolean isHolding = false;
public static int selected = 0;
public static int[] holdingID = Tile.air;
public static int holdingCount = 0;
public Inventory() {
for(int i = 0; i < invBar.length; i++) {
invBar[i] = new Cell(new Rectangle((Component.pixel.width / 2) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace))/2) + (i * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace), Tile.invCellSize, Tile.invCellSize), Tile.air);
}
int x = 0, y = 0;
for(int i = 0; i < invBag.length; i++) {
invBag[i] = new Cell(new Rectangle((Component.pixel.width / 2) - ((Tile.invLength * (Tile.invCellSize + Tile.invCellSpace))/2) + (x * (Tile.invCellSize + Tile.invCellSpace)), Component.pixel.height - (Tile.invCellSize + Tile.invBorderSpace) - (Tile.invHeight * (Tile.invCellSize + Tile.invCellSpace)) + (y * (Tile.invCellSize + Tile.invCellSpace)), Tile.invCellSize, Tile.invCellSize), Tile.air);
x++;
if(x == Tile.invLength) {
x = 0;
y++;
}
}
invBar[0].id = Tile.sand;
invBar[1].id = Tile.grass;
invBar[2].id = Tile.dirt;
invBar[3].id = Tile.bush;
}
public static void click(MouseEvent e) {
if(e.getButton() == 1) {
if(isOpen) {
for(int i = 0; 1 < invBar.length; i++) {
Line 43 > if(invBar[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))) {
if(invBar[i].id != Tile.air && !isHolding) {
holdingID = invBar[i].id;
invBar[i].id = Tile.air;
isHolding = true;
} else if(isHolding && invBar[i].id == Tile.air) {
invBar[i].id = holdingID;
isHolding = false;
} else if(isHolding && invBar[i].id != Tile.air) {
int[] con = invBar[i].id;
invBar[i].id = holdingID;
holdingID = con;
}
}
}
for(int i = 0; 1 < invBag.length; i++) {
if(invBag[i].contains(new Point(Component.mse.x / Component.pixelSize, Component.mse.y / Component.pixelSize))) {
if(invBag[i].id != Tile.air && !isHolding) {
holdingID = invBag[i].id;
invBag[i].id = Tile.air;
isHolding = true;
} else if(isHolding && invBag[i].id == Tile.air) {
invBag[i].id = holdingID;
isHolding = false;
} else if(isHolding && invBag[i].id != Tile.air) {
int[] con = invBag[i].id;
invBag[i].id = holdingID;
holdingID = con;
}
}
}
}
}
}
public void render(Graphics g) {
for(int i = 0; i < invBar.length; i++) {
boolean isSelected = false;
if(i == selected) {
isSelected = true;
}
invBar[i].render(g, isSelected);
}
if(isOpen) {
//g.fillRect(0, 0, 100, 100);
for(int i = 0;i < invBag.length; i++) {
invBag[i].render(g, false);
}
}
if(isHolding) {
// [- (Tile.invCellSize / 2)] centers item to mouse
g.drawImage(Tile.tileset_terrain, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invItemBorder, (Component.mse.x / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, (Component.mse.y / Component.pixelSize) - (Tile.invCellSize / 2) + Tile.invCellSize - Tile.invItemBorder, holdingID[0] * Tile.tileSize, holdingID[1] * Tile.tileSize, holdingID[0] * Tile.tileSize + Tile.tileSize, holdingID[1] * Tile.tileSize + Tile.tileSize, null);
}
}
}
提到的听力片段:
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
Component.isMseLeft = true;
} else if(e.getButton() == MouseEvent.BUTTON3) {
Component.isMseRight = true;
}
Line 158 > Inventory.click(e);
}
生成的错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at y.m.m.Inventory.click(Inventory.java:43)
at y.m.m.Listening.mousePressed(Listening.java:158)
at java.awt.Component.processMouseEvent(Component.java:6502)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
在for循环中,您有
int i = 0; 1 < invBag.length; i++
由于1 < invBag.length
始终为真,因此IndexOutOfBoundException