基本上是标题。我创建了一个更大的数组的新片段,我得到了这个错误。我必须创建一个更大阵列的新fragemnt来显示它。 (想想口袋妖怪,地图是大数组,但显示的屏幕是该地图的片段。)这是代码。
调用方法的地方:
public void showWorldFragment(){
******ERROR WITH THIS LINE*********
int[][] map = getWorldFragment(mapPack.map);
if(map[0][0] == 1){
c.drawBitmap(balloon, 0, 0, null);
}
if(map[0][0] == 2){
c.drawBitmap(archer, 0, 0, null);
}
if(map[1][0] == 1){
c.drawBitmap(balloon, canvasWidth/3, 0, null);
}
if(map[1][0] == 2){
c.drawBitmap(archer, canvasWidth/3, 0, null);
}
if(map[2][0] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), 0, null);
}
if(map[2][0] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), 0, null);
}
if(map[0][1] == 1){
c.drawBitmap(balloon, 0, canvasHeight/3, null);
}
if(map[0][1] == 2){
c.drawBitmap(archer, 0, canvasHeight/3, null);
}
if(map[1][1] == 1){
c.drawBitmap(balloon, canvasWidth/3, canvasHeight/3, null);
}
if(map[1][1] == 2){
c.drawBitmap(archer, canvasWidth/3, canvasHeight/3, null);
}
if(map[2][1] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), canvasHeight/3, null);
}
if(map[2][1] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), canvasHeight/3, null);
}
if(map[0][2] == 1){
c.drawBitmap(balloon, 0, 2*(canvasHeight/3), null);
}
if(map[0][2] == 2){
c.drawBitmap(archer, 0, 2*(canvasHeight/3), null);
}
if(map[1][2] == 1){
c.drawBitmap(balloon, (canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[1][2] == 2){
c.drawBitmap(archer, (canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[2][2] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[2][2] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
}
}
调用方法的地方:
public int[][] getWorldFragment(int[][] map) {
int centerX = x;
int centerY = y;
if(x == 0){
centerX = 1;
}
if(x == map.length){
centerX = map.length-1;
}
if(y == 0){
centerY = 1;
}
if(y == map.length){
centerY = map.length-1;
}
int[][] tempFragmentArray = new int[][]{
{map[centerX-1][centerY-1], map[centerX][centerY-1], map[centerX + 1][centerY - 1]},
{map[centerX-1][centerY], map[centerX][centerY], map[centerX + 1][centerY]},
{map[centerX-1][centerY+1], map[centerX][centerY+1], map[centerX + 1][centerY + 1]},
};
return tempFragmentArray;
}
mapPack.map是一个较大的5x5数组,只保存基本的整数值。有没有人知道为什么会这样?
如果需要更多详细信息,请与我们联系。
谢谢:)
答案 0 :(得分:1)
我认为内部异常来自这一行:
int[][] tempFragmentArray = new int[][]{
{map[centerX-1][centerY-1], map[centerX][centerY-1], map[centerX + 1][centerY - 1]},
{map[centerX-1][centerY], map[centerX][centerY], map[centerX + 1][centerY]},
{map[centerX-1][centerY+1], map[centerX][centerY+1], map[centerX + 1][centerY + 1]},
};
变量x和y看起来是内部类成员,可能它们会转到2D地图数组的错误索引。