日期:2014-05-20  浏览次数:20880 次

使用TiledLayer的setCell方法映射地图数组出现奇怪错误
//这是要映射的地图矩阵
private   static   final   int[][]   map   =   {  
{   0,   0,   0,   0   },  
{   0,   0,   0,   0   },
{   0,   0,   0,   0   }  
};
//我在构造函数中去映射这个地图数组
public   Sky()   {
          super(map[0].length,map.length,   getImage(),  
                      imgsrc.getWidth()/IMAGE_COLUMNS,  
                      imgsrc.getHeight()/IMAGE_ROWS);
//问题就出在这里,我在双循环中是用setCell()方法总报  
//IndexOutOfBoundsException的错误。
for(int   i=0;i <map.length;++i){
for(int   j=0;j <4;++j){
//但是我使用这条语句检查它的赋值情况又是完全正确的
System.out.println( "setCell "+ "( "+i+ ", "+j+ ") "+ "= "+map[i][j]);
//this.setCell(i,j,map[i][j]);

}
}
}
//这是System.out.println语句打印的结果:
setCell(0,0)=0
setCell(0,1)=0
setCell(0,2)=0
setCell(0,3)=0
setCell(1,0)=0
setCell(1,1)=0
setCell(1,2)=0
setCell(1,3)=0
setCell(2,0)=0
setCell(2,1)=0
setCell(2,2)=0
setCell(2,3)=0
//但是一旦使用setCell就报以下的错误很奇怪:
setCell(0,0)=0
setCell(0,1)=0
setCell(0,2)=0
setCell(0,3)=0
java.lang.IndexOutOfBoundsException
at   javax.microedition.lcdui.game.TiledLayer.setCell(+31)
at   ui.Sky. <init> (+107)
at   ui.GameThread.init(+8)
at   ui.GameThread. <init> (+27)
at   ui.MenuCanvas.LoadGame(+35)
at   ui.MenuCanvas.keyPressed(+101)
at   javax.microedition.lcdui.Canvas.callKeyPressed(+19)
at   javax.microedition.lcdui.Display$DisplayAccessor.keyEvent(+198)
at   javax.microedition.lcdui.Display$DisplayManagerImpl.keyEvent(+11)
at   com.sun.midp.lcdui.DefaultEventHandler.keyEvent(+127)
at   com.sun.midp.lcdui.AutomatedEventHandler.keyEvent(+210)
at   com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+122)
at   com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+51)
//望各位帮忙看看错误在那里啊?我查了一天还是没查出来

------解决方案--------------------
数组越界问题,仔细检查就会发现错误!

this.setCell(i,j,map[i][j]); 错了

改为: this.setCell(j,i,map[i][j]);