我正在制作游戏,但我还在制作屏幕制作过程中。 我做了一个黑色,深灰色,浅灰色和白色的spritesheet,我想让它们变成鲜艳的颜色,但是当我尝试初始化第一种颜色时,我得到一个ArryIndexOutOfBoundsExeption。
这是我需要帮助的课程的代码:
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH_MASK];
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH_MASK * 4];
public int xOffset = 0;
public int yOffset = 0;
public int width;
public int height;
public SpriteSheet sheet;
public Screen(int width, int height, SpriteSheet sheet){
this.width = width;
this.height = height;
this.sheet = sheet;
for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++ ){
colours[i*4+0] = 0xff00ff;
colours[i*4+1] = 0x00ffff;
colours[i*4+2] = 0xffff00;
colours[i*4+3] = 0xffffff;
}
}
public void Render(int[] pixels, int offset, int row){
for(int yTile = yOffset >> 3; yTile <=(yOffset + height) >> 3; yTile++){
int yMin = yTile * 8 - yOffset;
int yMax = yMin + 8;
if(yMin < 0) yMin = 0;
if(yMax > height) yMax = height;
for(int xTile = xOffset >> 3; xTile <=(xOffset + width) >> 3; xTile++){
int xMin = xTile * 8 - yOffset;
int xMax = xMin + 8;
if(xMin < 0) xMin = 0;
if(xMax > width) xMax = width;
int tileIndex = (xTile & (MAP_WIDTH_MASK)) + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH ;
for(int y = yMin; y < yMax; y++){
int sheetPixel = ((y + yOffset) & 7) * sheet.width + ((xMin + xOffset) & 7);
int tilePixel = offset + xMin + y * row;
for(int x = xMin; x < xMax; x++){
int colour = tileIndex * 4 + sheet.pixels[sheetPixel++];
pixels[tilePixel++] = colours[colour];
}
}
}
}
}
如果有人知道如何解决问题,以便该例程消失,请随时回复;)
答案 0 :(得分:0)
检查阵列的大小与您尝试访问的索引的对比。
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH_MASK * 4];
64 * 63 * 4 = 16128
计数器的最大值为64 * 64-1 = 4095
colours[i*4 + 3] == colours[4095*4 + 3]
4095 * 4 + 3 = 16,383
16,383&gt; 16,128,所以索引越界。