gpt4 book ai didi

java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 10 for Inventory

转载 作者:行者123 更新时间:2023-12-01 12:30:20 25 4
gpt4 key购买 nike

我一直在关注有关制作 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);
}
}
}

提到的听力 block :

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始终为 true ,因此 IndexOutOfBoundException

关于java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 10 for Inventory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com