我得到了一个奇怪的错误,我不知道为什么会有谁能发现错误在哪里?
错误:
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: -61
at ca.serict.game.gfx.Screen.render(Screen.java:55)
at ca.serict.game.entities.Player.render(Player.java:57)
at ca.serict.game.level.Level.renderEntities(Level.java:67)
at ca.serict.game.Game.render(Game.java:168)
at ca.serict.game.Game.run(Game.java:127)
at java.lang.Thread.run(Unknown Source)如果您需要查看这些行中的任何代码,请告诉我所列出的错误。
Screen.java 55线:
int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;Player.java 57线:
screen.render(xOffset, + modifier, yOffset, (xTile + 1) + yTile * 32, colour); Level.java 65-69号线:
public void renderEntities(Screen screen) {
for (Entity e : entities) {
e.render(screen);
}
}Game.java 168号线:
level.renderEntities(screen);Game.java 157-128号线:
if (shouldRender) {
frames++;
render();
}屏幕55的公开无效:
public void render(int xPos, int yPos, int tile, int colour, int mirrorDir) {
xPos -= xOffset;
yPos -= yOffset;
boolean mirrorX = (mirrorDir & BIT_MIRROR_X) > 0;
boolean mirrorY = (mirrorDir & BIT_MIRROR_Y) > 0;
int xTile = tile % 32;
int yTile = tile / 32;
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
for (int y = 0; y < 8; y++) {
if (y + yPos < -0 || y + yPos >= height)
continue;
int ySheet = y;
if (mirrorY)
ySheet = 7 - y;
for (int x = 0; x < 8; x++) {
if (x + xPos < -0 || x + xPos >= width)
continue;
int xSheet = x;
if (mirrorX)
xSheet = 7 - x;
int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
if (col < 255)
pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}发布于 2013-10-14 21:11:40
因此,您正在使用以下表达式计算数组索引:
xSheet + ySheet * sheet.width + tileOffset您需要确保该值在数组sheet.pixels的范围内。为此,您可以编写一个小方法来钳制索引:
public int clamp(int index, int start, int end) {
return index > end ? end : index < 0 ? 0 : index;
}然后像这样使用它:
int i = clamp((xSheet+ySheet*sheet.width+tileOffset), 0, sheet.pixels.length-1)
sheet.pixels[i];这样,您将确保在范围0,sheet.象素-1中有索引,但您仍然需要知道这对您的用例是否有意义。
发布于 2013-10-14 20:58:10
理想情况下,我们将拥有屏幕上的代码:55。但在我看来,您是在访问数组之外的一个元素。尝试打印在屏幕上访问的数组的大小:55,您应该会看到这个问题。
https://stackoverflow.com/questions/19369131
复制相似问题