2014-02-20 3 views
0

Я пытаюсь загрузить и отобразить уровни (плиточные карты) в Slick2D из текстовых файлов. Текстовые файлы выглядят примерно так:Почему не отображаются мои карты на плитки?

res/RPGSheet 32x32.png  (tileset to be used with this map) 
5       (How many tiles wide the map is) 
5       (How many tiles tall the map is) 
16       (How many tiles wide the tileset is) 
16       (How many tiles tall the tileset is) 
4,1 4,1 4,1 4,1 4,1 
4,1 1,1 2,0 4,1 4,1 
4,1 4,1 1,1 4,1 4,1 
4,1 1,1 4,1 1,1 4,1 
4,1 4,1 4,1 4,1 4,1 

Сначала я объясню, как работают мои tilemaps:

Каждая координата указывает, где плитка находится на Tileset изображения. Таким образом, 0,0 - это плитка в верхнем левом углу карты, 0,1 - плитка под ней, а 1,0 - плитка справа от нее.

Теперь проблема ...

По какой-то причине, tilemaps не оказывают на экран правильно. Я уверен, что он правильно читает файлы, но по какой-то причине он не делает их правильно. Например, выше tilemap должен показать, как это:

W W W W W 
W G D W W 
W W G W W 
W G W G W 
W W W W W 

(If W is a water tile, G is grass and D is dirt) 

Но вместо этого, он делает плитку на экране в этой формации:

W W W W W 
W G W G W 
W D G W W 
W W W G W 
W W W W W 

Почему это происходит?

Вот мой код:

public class Play extends BasicGameState{ 

int x; 
int y; 

Image image; 
SpriteSheet tileset; 

int MapWidth; 
int MapHeight; 

int TilesAcross; 
int TilesTall; 

int TileWidth; 
int TileHeight; 

String[] coords; 

int[] tilesX; 

int[] tilesY; 

String TileDir; 

Image tile; 

String map; 

int renderCount = 0; 

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{ 

     int count = 0; 

     map = new String("res/DefaultMap.txt"); 

     try(BufferedReader in = new BufferedReader(new FileReader(map))){ 
      String line; 

      TileDir = new String(in.readLine()); 
      MapWidth = Integer.parseInt(in.readLine()); 
      MapHeight = Integer.parseInt(in.readLine()); 
      TilesAcross = Integer.parseInt(in.readLine()); 
      TilesTall = Integer.parseInt(in.readLine()); 

      System.out.println("(DEBUG)Tileset Directory: " + TileDir); 
      System.out.println("(DEBUG)Map Width: " + MapWidth); 
      System.out.println("(DEBUG)Map Height: " + MapHeight); 
      System.out.println("(DEBUG)Tileset is " + TilesAcross + "x" + TilesTall + " Tiles"); 


      tilesX = new int[MapWidth * MapHeight]; 
      tilesY = new int[MapWidth * MapHeight]; 

      while((line=in.readLine())!=null){ 
       String[] values = line.split(" "); 
       for(String v:values){ 
        coords = v.split(","); 

        tilesX[count] = Integer.parseInt(coords[0]); 
        tilesY[count] = Integer.parseInt(coords[1]); 

        System.out.println("Coords: " + tilesX[count] + "," + tilesY[count]); 

        count++; 
       } 
      } 

       }catch(IOException e){ 
        e.printStackTrace(); 
       } 

       image = new Image(TileDir); 
       TileWidth = image.getWidth()/TilesAcross; 
       TileHeight = image.getHeight()/TilesTall; 
       tileset = new SpriteSheet(image, TileWidth, TileHeight); 
       System.out.println("(DEBUG)Tile Width: " + TileWidth); 
       System.out.println("(DEBUG)Tile Height: " + TileHeight); 

     } 

    public Play(int state){ 
    } 

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{ 
     tileset.startUse(); 
     for(renderCount = 0; renderCount < MapWidth * MapHeight;){ 
     for(int i = 0; i < MapWidth; i++){ 
      for(int j = 0; j < MapHeight; j++){ 
        tileset.getSubImage(tilesX[renderCount], tilesY[renderCount]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight); 
        renderCount++; 
      } 
      } 
     } 
     tileset.endUse(); 
    } 

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{ 
    } 

    public int getID(){ 
     return 3; 
    } 
} 

ответ

0
for(renderCount = 0; renderCount < MapWidth * MapHeight;){ 
    for(int i = 0; i < MapWidth; i++){ 
     for(int j = 0; j < MapHeight; j++){ 
       tileset.getSubImage(tilesX[renderCount], tilesY[renderCount]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight); 
       renderCount++; 
     } 
     } 

Это довольно странный кусок кода и, кажется, вы рендеринг плитки столбцов но массив расположен линейные мудр. Попробуйте заменить его:

for(int column = 0; line < MapHeight; column++) 
{ 
    for(int line = 0; column < MapWidth; line++) 
    { 
     tileset.getSubImage(tilesX[line*MapWidth + column], tilesY[line*MapWidth + column]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight); 
    } 
} 
+0

Я использовал свой код вместо этого, но вы ничего не заменить I и J в этой строке: 'tileset.getSubImage (tilesX [линия * MapWidth + столбец], [tilesY line * MapWidth + column]). drawEmbedded (i * TileWidth, j * TileHeight, TileWidth, TileHeight); 'поэтому я просто заменил i колонкой и j строкой, за исключением того, что игра вылетает, когда я вхожу в состояние Play. В чем проблема? Кроме того, btw я объявлял строку и столбец вне циклов FOR, потому что внешний цикл вызывает строку без ее объявления. – FidgetyTheGamer

+0

Хорошо, попробуйте просто заменить «MapWidth» на «MapHeight» в вашем исходном коде. – Machtl

+0

Затем он делает тот же странный способ, что и раньше. Кроме того, что-то важное, что я забыл упомянуть, заключается в том, что когда игра разбивается, как я сказал в своем предыдущем комментарии, она дает мне 'java.lang.ArrayIndexOutOfBoundsException: 25 \t на Base.Play.render (Play.java:112) ' – FidgetyTheGamer