0
在Platformer中,我使我需要加載瓷磚以便能夠創建關卡,但是在下面的代碼中,我似乎遇到了問題。它說,我在這個部分有一個錯誤:瓷磚地圖加載問題
String[] skips = skip.split(" ");
但它對我來說似乎很好,它總是工作過。有人可以給我一些見解,爲什麼這不起作用?
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);
}
}
}
}
謝謝!這實際上是一個很容易修復的錯誤,我不確定我是如何錯過的! – user2318396