2015-06-30 26 views
0

我正在嘗試創建一個非常簡單的瓷磚貼圖系統,幾周前我遇到了問題,並在此處詢問,但最近我已將其重寫並停止正常工作。爲什麼不能顯示整個瓷磚地圖?

請注意,我使用的是slick2D,所以如果你想重現這一點,那麼你必須把代碼放在你的主要渲染循環中。

陣列

public static int[][] map = {{1,1,1,1,1,2,1,1,1,1,1,1,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,0,0,0,0,0,0,0,0,0,0,0,1}, 
           {1,1,1,1,1,1,1,0,0,0,0,0,1}}; 

瓷磚地圖循環。

int x = 0; 
int y = 0; 
int I = 0; 
int II = 0; 
while(y <= 7){ 
    while(x <= 12){ 
     if(map[y][x] == 0){ 
      Image img = new Image("res/tile1.png"); 
      img.draw(II,I); 
     } 
     if(map[y][x] == 1){ 
      Image img = new Image("res/tile0.png"); 
      img.draw(II,I); 
     } 
     if(map[y][x] == 2){ 
      Image img = new Image("res/tile3.jpg"); 
      img.draw(II,I); 
     } 
     x++; 
     II = x * 100; 
    } 
    y++; 
    I = y * 100; 
} 

截圖http://puu.sh/iIf9r/42c3b6f4db.png

感謝。

+0

繪製方法是自定義的嗎?從快速檢查中,環路應該正常運行。再來看看,我下面的答案是正確的。 –

回答

3

從我的理解你的代碼你喜歡打印你的圖像在矩形7x12。 如果我深知你的每一行之前重置X如此前while(x <= 12){

int x = 0; 
int y = 0; 
int I = 0; 
int II = 0; 
while(y <= 7){ 
    x = 0; 
    while(x <= 12){ 
     if(map[y][x] == 0){ 
      Image img = new Image("res/tile1.png"); 
      img.draw(II,I); 
     } 
     if(map[y][x] == 1){ 
      Image img = new Image("res/tile0.png"); 
      img.draw(II,I); 
     } 
     if(map[y][x] == 2){ 
      Image img = new Image("res/tile3.jpg"); 
      img.draw(II,I); 
     } 
     x++; 
     II = x * 100; 
    } 
    y++; 
    I = y * 100; 
} 

注意有沒有需要一個更好的性能,每次創建新的圖像。創建瓷磚並重新使用它們。

1

這似乎是達維德的答案是正確的,但我想提醒你注意,你可以做一個優化的一點點。

目前在你的循環,你正在檢查,如果你的瓦等於某個值:

if(map[y][x] == 0) 
... 
if(map[y][x] == 1) 
... 
etc 

這是一切都很好,直到你有瓷磚的數百或數千。所有這些if語句都在做確切的事情,即加載平鋪圖像並繪製它。但是,出於速度的考慮,這不是主要問題。當您知道最終結果是什麼時,其中一個主要問題是每次迭代初始化圖像。我不熟悉的​​,但你可能會做這樣的事情:

// Initialize this once outside your render loop 
Image[] tileImages = { 
    new Image("res/tile1.png"), 
    new Image("res/tile0.png"), 
    new Image("res/tile3.png") 
}; 

... 

int x = 0; 
int y = 0; 
int I = 0; 
int II = 0; 
while(y <= 7){ 
    x = 0; 
    while(x <= 12){ 
     // This replaces a lot of unnecessary code and makes it more efficient 
     tileImages[map[y][x]].draw(II, I); 
     x++; 
     II = x * 100; 
    } 
    y++; 
    I = y * 100; 
} 

注:我沒有測試過這一點,但總的想法是存在的。另外,我把達維德的關於設置的x = 0值原代碼,並用此方法修改它。

+0

感謝您的輸入! – CalebB