Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不';我的平铺贴图显示不正确吗?_Java_2d_Tile_Slick2d - Fatal编程技术网

Java 为什么不';我的平铺贴图显示不正确吗?

Java 为什么不';我的平铺贴图显示不正确吗?,java,2d,tile,slick2d,Java,2d,Tile,Slick2d,我正在尝试从文本文件加载和渲染Slick2D中的级别(平铺贴图)。文本文件如下所示: res/RPGSheet 32x32.png (tileset to be used with this map) 5 (How many tiles wide the map is) 5 (How many tiles tall the map is) 16

我正在尝试从文本文件加载和渲染Slick2D中的级别(平铺贴图)。文本文件如下所示:

res/RPGSheet 32x32.png     (tileset to be used with this map)
5                          (How many tiles wide the map is)
5                          (How many tiles tall the map is)
16                         (How many tiles wide the tileset is)
16                         (How many tiles tall the tileset is)
4,1 4,1 4,1 4,1 4,1
4,1 1,1 2,0 4,1 4,1
4,1 4,1 1,1 4,1 4,1
4,1 1,1 4,1 1,1 4,1
4,1 4,1 4,1 4,1 4,1
 W W W W W
 W G D W W
 W W G W W
 W G W G W
 W W W W W

 (If W is a water tile, G is grass and D is dirt)
首先,我将解释我的tilemaps是如何工作的:

每个坐标指示平铺在平铺集图像上的位置。因此,0,0是地图左上角的平铺,0,1是地图下方的平铺,1,0是地图右侧的平铺

现在来看问题

由于某些原因,tilemaps无法正确渲染到屏幕上。我确信它正在正确地读取文件,但由于某些原因,它无法正确地呈现它们。例如,上面的tilemap应显示如下:

res/RPGSheet 32x32.png     (tileset to be used with this map)
5                          (How many tiles wide the map is)
5                          (How many tiles tall the map is)
16                         (How many tiles wide the tileset is)
16                         (How many tiles tall the tileset is)
4,1 4,1 4,1 4,1 4,1
4,1 1,1 2,0 4,1 4,1
4,1 4,1 1,1 4,1 4,1
4,1 1,1 4,1 1,1 4,1
4,1 4,1 4,1 4,1 4,1
 W W W W W
 W G D W W
 W W G W W
 W G W G W
 W W W W W

 (If W is a water tile, G is grass and D is dirt)
但是,它会以这种形式将瓷砖渲染到屏幕上:

 W W W W W
 W G W G W
 W D G W W
 W W W G W
 W W W W W
为什么会发生这种情况

这是我的密码:

public class Play extends BasicGameState{

int x;
int y;

Image image;
SpriteSheet tileset;

int MapWidth;
int MapHeight;

int TilesAcross;
int TilesTall;

int TileWidth;
int TileHeight;

String[] coords;

int[] tilesX;

int[] tilesY;

String TileDir;

Image tile;

String map;

int renderCount = 0;

   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{

       int count = 0;

       map = new String("res/DefaultMap.txt");

        try(BufferedReader in = new BufferedReader(new FileReader(map))){
            String line;

            TileDir = new String(in.readLine());
            MapWidth = Integer.parseInt(in.readLine());
            MapHeight = Integer.parseInt(in.readLine());
            TilesAcross = Integer.parseInt(in.readLine());
            TilesTall = Integer.parseInt(in.readLine());

            System.out.println("(DEBUG)Tileset Directory: " + TileDir);
            System.out.println("(DEBUG)Map Width: " + MapWidth);
            System.out.println("(DEBUG)Map Height: " + MapHeight);
            System.out.println("(DEBUG)Tileset is " + TilesAcross + "x" + TilesTall + " Tiles");


            tilesX = new int[MapWidth * MapHeight];
            tilesY = new int[MapWidth * MapHeight];

            while((line=in.readLine())!=null){
                String[] values = line.split(" ");
                for(String v:values){
                    coords = v.split(",");

                    tilesX[count] = Integer.parseInt(coords[0]);
                    tilesY[count] = Integer.parseInt(coords[1]);

                    System.out.println("Coords: " + tilesX[count] + "," + tilesY[count]);

                    count++;
                }
            }

                }catch(IOException e){
                    e.printStackTrace();
                }

                image = new Image(TileDir);
                TileWidth = image.getWidth() / TilesAcross;
                TileHeight = image.getHeight() / TilesTall;
                tileset = new SpriteSheet(image, TileWidth, TileHeight);
                System.out.println("(DEBUG)Tile Width: " + TileWidth);
                System.out.println("(DEBUG)Tile Height: " + TileHeight);

        }

   public Play(int state){
   }

   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
       tileset.startUse();
       for(renderCount = 0; renderCount < MapWidth * MapHeight;){
       for(int i = 0; i < MapWidth; i++){
           for(int j = 0; j < MapHeight; j++){
                   tileset.getSubImage(tilesX[renderCount], tilesY[renderCount]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight);
                   renderCount++;
           }
           }
       }
       tileset.endUse();
   }

   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
   }

   public int getID(){
      return 3;
   }
}
public class Play扩展了BasicGameState{
int x;
int-y;
图像;
雪碧片瓷砖;
int地图宽度;
int地图高度;
int TilesAcross;
int TilesTall;
int瓦宽;
整数分位数;
字符串[]坐标;
int[]tilex;
int[]tilesY;
字符串TileDir;
图像块;
字符串映射;
int renderCount=0;
public void init(GameContainer gc,StateBasedGame sbg)引发异常{
整数计数=0;
map=新字符串(“res/DefaultMap.txt”);
try(BufferedReader in=new BufferedReader(new FileReader(map))){
弦线;
TileDir=新字符串(in.readLine());
MapWidth=Integer.parseInt(in.readLine());
MapHeight=Integer.parseInt(in.readLine());
TilesAcross=Integer.parseInt(in.readLine());
TilesTall=Integer.parseInt(in.readLine());
System.out.println(“(调试)Tileset目录:”+TileDir);
System.out.println(“(调试)映射宽度:”+MapWidth);
System.out.println(“(调试)映射高度:”+MapHeight);
System.out.println(“(调试)Tileset为”+TilesAcross+“x”+TilesTall+“Tiles”);
tilex=新整数[MapWidth*MapHeight];
tilesY=新整数[MapWidth*MapHeight];
而((line=in.readLine())!=null){
字符串[]值=行。拆分(“”);
用于(字符串v:值){
coords=v.分割(“,”);
tilex[count]=Integer.parseInt(coords[0]);
tilesY[count]=Integer.parseInt(coords[1]);
System.out.println(“坐标:“+tilex[count]+”,“+tilesY[count]);
计数++;
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
图像=新图像(TileDir);
TileWidth=image.getWidth()/TilesAcross;
TileHeight=image.getHeight()/TilesTall;
tileset=新的子画面(图像、平铺宽度、平铺高度);
System.out.println(“(调试)平铺宽度:”+TileWidth);
System.out.println(“(调试)磁贴高度:“+TileHeight”);
}
公共游戏(国际国家){
}
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常{
tileset.startUse();
对于(renderCount=0;renderCount
for(renderCount=0;renderCount
这是一段非常奇怪的代码,看起来您正在按列渲染平铺,但数组是按行排列的。请尝试将其替换为:

for(int column = 0; line < MapHeight; column++)
{
    for(int line = 0; column < MapWidth; line++)
    {
        tileset.getSubImage(tilesX[line*MapWidth + column], tilesY[line*MapWidth + column]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight);
    }
}
for(int column=0;line
我使用了您的代码,但您没有用任何东西替换此行中的I和j:
tileset.getSubImage(tilex[line*MapWidth+column],tilesY[line*MapWidth+column])。抽屉嵌入(I*TileWidth,j*TileHeight,TileWidth,TileHeight)
所以我用列替换了I,用行替换了j,但现在当我进入游戏状态时游戏崩溃了。问题是什么?另外,顺便说一句,我在FOR循环之外声明了行和列,因为外部循环调用line而没有声明它。好的,试着简单地用原始数据中的
MapHeight
交换
l代码。然后它以与最初相同的奇怪方式呈现。另外,我忘了提及的一件重要事情是,当游戏崩溃时,正如我在前面的评论中所说,它给了我一个
java.lang.ArrayIndexOutOfBoundsException:25 at Base.Play.render(Play.java:112)
您还需要在和仅在
抽屉嵌入(…)中交换
i
j