我正在尝试制作RPG游戏,而现在,我正在尝试从文本文件中加载地图。我复制并粘贴了第二层和第三层的第一层代码,但它只适用于第一层。当它尝试在第二层中创建一个新的BasicTile时,我得到一个null异常错误。根据错误,arrayId和imageId为null,但我无法理解为什么。请帮忙,谢谢!!!
//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 Class继承的。
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中的错误,如果有帮助的话。
答案 0 :(得分:1)
当调用Integer.parseInt
并返回null
时,这意味着该字符串的长度为0。在将它们传递给parseInt
方法之前,我会检查数组的元素。
仅供参考,这是docs的相关部分:
第一个参数为null或是一个长度为零的字符串。