简体   繁体   English

Java Tiled地图正在横向绘制

[英]Java Tiled map is painting sideways

Recently I've been trying to make a little game for my learning purpose but I got stuck. 最近,我一直在尝试为自己的学习目的制作一个小游戏,但是我陷入了困境。 I was able to generate a map from a .txt file but then it seems to paint it sideways? 我可以从.txt文件生成地图,但是似乎将其横向绘制了吗?

this is my map loaded which reads from a text file and add them to an ArrayList. 这是我加载的地图,该地图从文本文件读取并将其添加到ArrayList。 Width is 20 Height is 10. Same thing applies in my map 20 lines wide 10 down. 宽度为20高度为10。同样的情况适用于我的地图,向下20行宽20行。

public void loadMap(String name) {
        try {
            this.name = name;

            FileInputStream file_stream = new FileInputStream(name);
            DataInputStream in = new DataInputStream(file_stream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            map = new Tile[WIDTH][HEIGHT];

            String line;
            int y = 0;
            while ((line = br.readLine()) != null) {
                String[] tokens = line.split(" ");
                for (int x = 0; x < WIDTH; x++) {
                    boolean f = false;
                    if (Integer.parseInt(tokens[x]) == 1)
                        f = true;
                    Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x]));
                    tiles.add(tile);
                    map[x][y] = tile;
                    System.out.println("adding tile (x: " + x + " y: " + y + ") " + f + " " + Integer.parseInt(tokens[x]));
                }
                y++;
            }
            in.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "The map: " + name + " was not found.");
            e.printStackTrace();
        }
    }

This is the draw method for the map 这是地图的绘制方法

public void paint(Graphics2D g) {
        for (Tile t : getTiles()) {
            t.draw(g);
        //g.draw(t.getBounds());
        }
    }

Here is an example map 这是一个示例地图

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

The way it paints the map is basically a vertical version of this instead of the correct way like it is above. 它绘制地图的方式基本上是这种方式的垂直版本,而不是上面的正确方式。 I have tried messing with the numbers within the mapLoaded (width, height) and switching the x and y around but nothing seems to work. 我试图弄乱mapLoaded中的数字(宽度,高度)并在x和y之间切换,但似乎没有任何效果。 Please help 请帮忙

You simply need to change Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x])); 您只需要更改Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x])); to Tile tile = new Tile(x, y, f, Integer.parseInt(tokens[x])); to Tile tile = new Tile(x, y, f, Integer.parseInt(tokens[x])); .

Everytime you're making a new Tile, you're switching x and y which is causing your problems. 每次制作新的图块时,都会切换x和y,这会引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM