2017-05-08 104 views
0

這樣我就可以使用使用文本文件,它是偉大的,所有的瓷磚加載二維地圖。然而,我遇到的這個方法遇到的一個問題是,由於文件是2D網格,因此我無法將對象/演員添加到我的地圖。 (遊戲是類似遊戲,如塞爾達和寵物小精靈。)我嘗試創建一個對象層,這樣我就可以重疊的圖像,但它似乎並沒有爲我工作。舉一個我想要的例子,讓樹木等物體在背景草地上變得堅實。 我也在尋找更好的方法來創建這些基於區塊的地圖,如果你想一些想法推銷給我。地圖(2D Java引擎)標準庫

**注:我對初學者/ Java的在中間。

這裏是我的調用地圖的遊戲狀態類的構造函數。

public GameState(Game game) { 
    super(game); 
    player = new Player(game, 0, 0, 64, 64); 
    map = new Map(game, "res/saves/save1.txt"); 
} 

這是Map類(它的工作),它也調用對象(第2層)。

private int width, height; 
public static int spawnX, spawnY; 
private int[][] mapTiles; 
MapObjects mapObjects; 
Game game; 

public Map(Game game, String path) { 
    this.game = game; 
    mapObjects = new MapObjects(game, "res/saves/save1_obj.txt", width, height); 
    loadMap(path); 
} 

private void loadMap(String path) { 
    String file = Utils.loadFileAsString(path); 
    //Token is which number it is out of the total 
    String[] tokens = file.split("\\s+"); 
    //Sets what is what 
    width = Utils.parseInt(tokens[0]); 
    height = Utils.parseInt(tokens[1]); 
    spawnX = Utils.parseInt(tokens[2]); 
    spawnY = Utils.parseInt(tokens[3]); 

    mapTiles = new int[width][height]; 
    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      // (x+y*width) : calculates the nth token (+4) : The 4 prior tokens before the graph 
      mapTiles[x][y] = Utils.parseInt(tokens[(x + y *width) + 4]); 
     } 
    } 

} 

public void render(Graphics g) { 
    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      //Only renders what is seen. 
      getMapTile(x, y).render(g, (int)(x*Tile.TILE_WIDTH-game.getCamera().getxOffset()), (int)(y*Tile.TILE_HEIGHT-game.getCamera().getyOffset())); 
     } 
    } 
} 

public void tick() { 

} 

//Gets the specific tile at specific coordinates. 
private Tile getMapTile(int x, int y) { 
    Tile t = Tile.tiles[mapTiles[x][y]]; 
    if(t == null) { 
     return Tile.grassTile; 
    } 
    return t; 
} 

最後,對象層不起作用。它不會給出錯誤,只是重疊的對象不可見。我已經確保在Map層之前加載對象層,但這似乎不是問題。

private int width, height; 
private int[][] objTiles; 
Game game; 

public MapObjects(Game game, String path, int width, int height) { 
    this.game = game; 
    loadObjects(path, width, height); 
} 

public void loadObjects(String path, int width, int height) { 
    this.width = width; 
    this.height = height; 

    String file = Utils.loadFileAsString(path); 
    String[] tokens = file.split("\\s+"); 

    objTiles = new int[width][height]; 
    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      objTiles[x][y] = Utils.parseInt(tokens[(x + y *width)]); 
     } 
    } 
} 

public void render(Graphics g) { 
    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      //Only renders what is seen. 
      getObjTile(x, y).render(g, (int)(x*Tile.TILE_WIDTH-game.getCamera().getxOffset()), (int)(y*Tile.TILE_HEIGHT-game.getCamera().getyOffset())); 
     } 
    } 
} 

public void tick() { 

} 

//Gets the specific object tile at specific coordinates. 
private Tile getObjTile(int x, int y) { 
    Tile t = Tile.tiles[objTiles[x][y]]; 
    if(t == null) { 
     return Tile.nothingTile; 
    } 
    return t; 
} 

回答

0

我們可能需要更多信息給您。

您是否使用不同的「容器/組件」來繪製地圖圖塊而不是您的對象?因爲如果你先渲染對象,那麼渲染地圖時就會消失。你應該先繪製地圖,然後執行對象,像這樣:

public Map(Game game, String path) { 
    this.game = game; 
    //swapped the order of the lines below so the map loads first: 
    loadMap(path); 
    mapObjects = new MapObjects(game, "res/saves/save1_obj.txt", width, height); 
} 

從你所說的那麼這也不管用,但是如果你使用相同的組件來繪製地圖和對象然後一個永遠重寫另一個,並且總是會丟失一些東西。要解決這個問題,您需要摺疊兩個單獨的窗格,一個用於地圖,另一個透明窗格位於地圖頂部,您可以用來渲染對象。

見該圖中作爲一個例子:

Root Panes Example

你基本上需要添加新的透明的「內容平面」類似於上面所示的玻璃板的方式。

+0

嗯我切換順序,呃時刻 - 不知道爲什麼我認爲我按正確的順序,但我沒有,但是對象層仍然沒有加載。我要去研究它,也許試着重寫整個事情,看看我能否想出不同的東西。謝謝你的解釋和那個整齊的圖像! –