简体   繁体   English

平铺地图加载问题

[英]Tile Map Loading Issues

In the Platformer I am making I need to load tiles in order to be able to create levels, but in the code below I seem to be having problems. 在Platformer中,我需要加载切片才能创建级别,但是在下面的代码中,我似乎遇到了问题。 It says that I have an error at this part: 它说我在这一部分有一个错误:

String[] skips = skip.split(" ");

but it seems fine to me, and it's always worked before. 但对我来说似乎还不错,并且一直以来都可以使用。 Can somebody maybe give me some insight as to why this is not working? 有人可以给我一些为什么不能奏效的见解吗?

Dungeon.java 地牢

package ScreenContents;

import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Dungeon {


private static int width;
private static int height;
private static final int tileSize = 32;
private int[][] map;

public void readMap(String location){
    URL url = getClass().getResource(location);
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

        width = Integer.parseInt(reader.readLine());
        height = Integer.parseInt(reader.readLine());
        map = new int[height][width];

        for (int y = 0; y < height; y++){
            String skip = reader.readLine();
            String[] skips = skip.split(" ");
            for (int x = 0; x < width; x++){
                map[y][x] = Integer.parseInt(skips[x]);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void renderMap(Graphics g){
    for (int y = 0; y < height; y++){
        for (int x = 0; x < width; x++){
            int newMapPos = map[y][x];

            if (newMapPos == 0){
                g.setColor(Color.black);
            }

            if  (newMapPos == 1){
                g.setColor(Color.white);
            }

            g.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);

        }
    }
}

}

The line: String[] skips = skip.split(" "); 该行: String[] skips = skip.split(" "); has skip set to null. 将skip设置为null。

This is because reader.readLine(); 这是因为reader.readLine(); returned null. 返回null。

Looking at the documentation "A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached". 查看文档 “包含行内容的字符串,不包括任何行终止符;如果已到达流的末尾,则为null”。

You are basically reading too many lines from your file, which means the height in your file does not match the number of lines that are actually in the file. 基本上,您从文件中读取的行太多,这意味着文件中的高度与文件中实际的行数不匹配。

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

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