Slick Forums
http://slick.ninjacave.com/forum/

TiledMapPlus empty tile outofboundsexception
http://slick.ninjacave.com/forum/viewtopic.php?f=1&t=6071
Page 1 of 1

Author:  rallu [ Mon Jan 07, 2013 5:19 pm ]
Post subject:  TiledMapPlus empty tile outofboundsexception

When loading tiled maps layers where there is no tile set for slots getTiles() will crash with array out of bounds exception.

Here is problem:

Original code Layer.java line 342
Code:
   public ArrayList<Tile> getTiles() throws SlickException {
      if (tmap == null) {
         throw new SlickException(
               "This method can only be used with Layers loaded using TiledMapPlus");
      }
      ArrayList<Tile> tiles = new ArrayList<Tile>();
      for (int x = 0; x < this.width; x++) {
         for (int y = 0; y < this.height; y++) {
                    String tilesetName = tmap.tileSets.get(this.data[x][y][0]).name;
                    Tile t = new Tile(x, y, this.name, y, tilesetName);
                    tiles.add(t);
         }
      }
      return tiles;
   }


And here is fixed method:

Code:
   public ArrayList<Tile> getTiles() throws SlickException {
      if (tmap == null) {
         throw new SlickException(
               "This method can only be used with Layers loaded using TiledMapPlus");
      }
      ArrayList<Tile> tiles = new ArrayList<Tile>();
      for (int x = 0; x < this.width; x++) {
         for (int y = 0; y < this.height; y++) {
                int index = this.data[x][y][0];
                if (index != -1) {
                    String tilesetName = tmap.tileSets.get(index).name;
                    Tile t = new Tile(x, y, this.name, y, tilesetName);
                    tiles.add(t);
                }
         }
      }
      return tiles;
   }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/