在我正在制作的 Platformer 中,我需要加载图块才能创建关卡,但在下面的代码中,我似乎遇到了问题。它说我在这部分有一个错误:
String[] skips = skip.split(" ");
但这对我来说似乎很好,而且以前一直有效。有人可以给我一些关于为什么这不起作用的见解吗?
地牢.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);
}
}
}
}