-1
我想做一個RPG遊戲,現在,我試圖從文本文件加載地圖。我複製並粘貼了第二層和第三層的第一層代碼,但它只適用於第一層。當它試圖在第二層中創建一個新的BasicTile時,我得到一個空的異常錯誤。根據錯誤,arrayId和imageId爲空,但我不明白爲什麼。請幫忙,謝謝!Java說他們剛剛工作的整數是空的
//MapLoader Function
public void load(String path){
mapLayer1 = new Tile[tilesWidth + 1][tilesHeight + 1];
mapLayer2 = new Tile[tilesWidth + 1][tilesHeight + 1];
mapLayer3 = new Tile[tilesWidth + 1][tilesHeight + 1];
try{
InputStream in = getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int y = 0;
while((line = br.readLine()) != null){ //Tile Layer 1
if(line.equalsIgnoreCase("[LAYERTWO]")) break;
String[] val = line.split(" ");
for(int x = 0; x < tilesWidth; x++){
String[] value = val[x].split(",");
int arrayId = Integer.parseInt(value[0]);
int imageId = Integer.parseInt(value[1]);
if(arrayId == 0 && imageId == 4){ //Water
Tile tile = new AnimatedTile(x, y, arrayId, imageId, new int[] {0,1,2,1}, 750);
tilesLayer1.add(tile);
mapLayer1[x][y] = tile;
}else{
Tile tile = new BasicTile(x, y, arrayId, imageId);
tilesLayer1.add(tile);
mapLayer1[x][y] = tile;
}
}
y++;
}
y = 0;
while((line = br.readLine()) != null){ //Tile Layer 2
if(line.equalsIgnoreCase("[LAYERTHREE]")) break;
String[] val = line.split(" ");
for(int x = 0; x < tilesWidth; x++){
String[] value = val[x].split(",");
int arrayId = Integer.parseInt(value[0]);
int imageId = Integer.parseInt(value[1]);
if(arrayId == 0 && imageId == 4){ //Water
Tile tile = new AnimatedTile(x, y, arrayId, imageId, new int[] {0,1,2,1}, 750);
tilesLayer2.add(tile);
mapLayer2[x][y] = tile;
}else{
Tile tile = new BasicTile(x, y, arrayId, imageId);
tilesLayer2.add(tile);
mapLayer2[x][y] = tile;
}
}
y++;
}
y = 0;
while((line = br.readLine()) != null){ //Tile Layer 3
if(line.equalsIgnoreCase("[COLLIDERS]")) break;
String[] val = line.split(" ");
for(int x = 0; x < tilesWidth; x++){
String[] value = val[x].split(",");
int arrayId = Integer.parseInt(value[0]);
int imageId = Integer.parseInt(value[1]);
if(arrayId == 0 && imageId == 4){ //Water
Tile tile = new AnimatedTile(x, y, arrayId, imageId, new int[] {0,1,2,1}, 750);
tilesLayer3.add(tile);
mapLayer3[x][y] = tile;
}else{
Tile tile = new BasicTile(x, y, arrayId, imageId);
tilesLayer3.add(tile);
mapLayer3[x][y] = tile;
}
}
y++;
}
while((line = br.readLine()) != null){ //Colliders
String[] val = line.split(" ");
colliders.add(new Rectangle(Integer.parseInt(val[0]), Integer.parseInt(val[1]), Integer.parseInt(val[2]), Integer.parseInt(val[3])));
}
in.close();
}catch(IOException e){
e.printStackTrace();
}
}
然後這是BaseTile類。
//BasicTile Class
public class BasicTile extends Tile{
public BasicTile(int tileX, int tileY, int arrayId, int imageId) {
super(tileX, tileY, arrayId, imageId);
}
public void update() {}
public void render() {
Handler.g.drawImage(image, tileX * Tile.WIDTH, tileY * Tile.HEIGHT, tileWidth * Tile.WIDTH, tileHeight * Tile.HEIGHT, null);
}
}
這就是BasicTile繼承的Tile類。
public abstract class Tile {
public static final int WIDTH = 32;
public static final int HEIGHT = 32;
protected int tileX;
protected int tileY;
protected int tileWidth;
protected int tileHeight;
protected int arrayId;
protected int imageId;
protected BufferedImage image;
public Tile(int tileX, int tileY, int arrayId, int imageId){
this.tileX = tileX;
this.tileY = tileY;
this.arrayId = arrayId;
this.imageId = imageId;
switch(arrayId){
case 0:
this.image = ImageHandler.getTileImage(imageId);
this.tileWidth = this.image.getWidth()/Tile.WIDTH; //This is where the error came up in the console.
this.tileHeight = this.image.getHeight()/Tile.HEIGHT;
break;
}
}
public abstract void update();
public abstract void render();
}
再次感謝您的幫助。我知道這是很多東西要看,但我不知道你們都需要什麼。我評論說我在Tile Class中遇到錯誤,如果有幫助的話。
謝謝@SimplicityGuy。現在它將字符串「0,3」從逗號分割出來,所以我只是想它會抓住第一個和第二個元素,像第一個層次一樣,非常漂亮並且非常隨意。 xD – Ctsmario 2015-04-05 02:09:23
@Ctsmario:你是否嘗試在這些行上設置一個斷點並驗證這些值是否你期望它們是?你說得對,字符串「0,3」你應該看到values [0] = 0和values [1] = 3,但是驗證數據是你真正期望的是很好的。 – SimplicityGuy 2015-04-05 02:12:23
我沒有,但我現在。你只是想通過「設置斷點」在整個代碼中添加可能的System.out.printlns?我對Java很新,所以我不太瞭解這些技術性的話題。對於那個很抱歉。 – Ctsmario 2015-04-05 02:18:02