2015-04-05 25 views
-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中遇到错误,如果有帮助的话。

回答

1

当调用Integer.parseInt并返回null时,这意味着该字符串的长度为0。在将它们传递给parseInt方法之前,我会检查数组中的元素。

仅供参考,这里是从docs的相关部分:

第一个参数是空值或零长度的字符串。

+0

谢谢@SimplicityGuy。现在它将字符串“0,3”从逗号分割出来,所以我只是想它会抓住第一个和第二个元素,像第一个层次一样,非常漂亮并且非常随意。 xD – Ctsmario 2015-04-05 02:09:23

+0

@Ctsmario:你是否尝试在这些行上设置一个断点并验证这些值是否你期望它们是?你说得对,字符串“0,3”你应该看到values [0] = 0和values [1] = 3,但是验证数据是你真正期望的是很好的。 – SimplicityGuy 2015-04-05 02:12:23

+0

我没有,但我现在。你只是想通过“设置断点”在整个代码中添加可能的System.out.printlns?我对Java很新,所以我不太了解这些技术性的话题。对于那个很抱歉。 – Ctsmario 2015-04-05 02:18:02