2012-01-03 97 views
0

該腳本繪製地圖和其他的東西:瓷磚基於地圖滾動

public void render(Canvas canvas) { 
     canvas.drawColor(Color.TRANSPARENT); 

     Drawable myImage; 

     int tileWidth = 50; 
     int tileHeight = 50; 

     int mapWidth = 3; 
     int mapHeight = 3; 

     int rowBaseX = 0; 
     int rowBaseY = 0; 

     int[][] board = new int[][] { 
       {0,0,0}, 
       {0,0,0}, 
       {0,0,2} 
       }; 

     for (int row = 0; row < mapHeight; row++) 
     { 

     for (int col = 0; col < mapWidth; col++) 
     { 
     Resources res = this.getContext().getResources(); 

     switch(board[row][col]) 
     { 
     case 0: 
     myImage = res.getDrawable(R.drawable.tile1); 
     break; 
     case 1: 
     myImage = res.getDrawable(R.drawable.tile2); 
     break; 
     default: 
     myImage = res.getDrawable(R.drawable.tile3); 
     break; 
     } 

     int curL; 
     int curU; 

     int curR; 
     int curD; 

     curL = rowBaseX + (col * tileWidth); 
     curU = rowBaseY + (row * tileHeight); 

     curR = curL + tileWidth; 
     curD = curU + tileHeight; 

     if (droid.x - decentreX < curR & droid.x + decentreX > curL) { 
      if (droid.y - decentreY < curD & droid.y + decentreY > curU) { 
     myImage.setBounds(curL,curU,curR,curD); 
     myImage.draw(canvas); 
      } 
     } 
     } 
     } 

     droid.draw(canvas); 
     butt.draw(canvas); 
     butt1.draw(canvas); 
     butt2.draw(canvas); 
     butt3.draw(canvas); 
     buttz.draw(canvas); 
     buttz1.draw(canvas); 
     buttz2.draw(canvas); 
     buttz3.draw(canvas); 
     buttx.draw(canvas); 
    } 

渲染(帆布油畫)methos被稱爲每一幀上。我怎樣才能滾動地圖瓷磚?我試過這個:

public void render(Canvas canvas) { 
     canvas.drawColor(Color.TRANSPARENT); 

     Drawable myImage; 

     int tileWidth = 50; 
     int tileHeight = 50; 

     int mapWidth = 3; 
     int mapHeight = 3; 

     int rowBaseX = 0; 
     int rowBaseY = 0; 

     int[][] board = new int[][] { 
       {0,0,0}, 
       {0,0,0}, 
       {0,0,2} 
       }; 

     for (int row = 0; row < mapHeight; row++) 
     { 

     for (int col = 0; col < mapWidth; col++) 
     { 
     Resources res = this.getContext().getResources(); 

     switch(board[row][col]) 
     { 
     case 0: 
     myImage = res.getDrawable(R.drawable.tile1); 
     break; 
     case 1: 
     myImage = res.getDrawable(R.drawable.tile2); 
     break; 
     default: 
     myImage = res.getDrawable(R.drawable.tile3); 
     break; 
     } 

     int curL; 
     int curU; 

     int curR; 
     int curD; 

     curL = rowBaseX + (col * tileWidth); 
     curU = rowBaseY + (row * tileHeight); 

     if (droid.touched & !droid.touched1 & !droid.touched3) { 
      curL -= 1; 
     }else if (droid.touched1 & !droid.touched & !droid.touched2){ 
      curU += 1; 
     }else if (droid.touched2 & !droid.touched1 & !droid.touched3){ 
      curL += 1; 
     }else if (droid.touched3 & !droid.touched2 & !droid.touched){ 
      curU -= 1; 
     }else if (droid.touched & droid.touched1){ 
      curL -= 1; 
      curU += 1; 
     }else if (droid.touched1 & droid.touched2){ 
      curL += 1; 
      curU += 1; 
     }else if (droid.touched2 & droid.touched3){ 
      curL += 1; 
      curU -= 1; 
     }else if (droid.touched3 & droid.touched){ 
      curL -= 1; 
      curU -= 1; 
     } 

     curR = curL + tileWidth; 
     curD = curU + tileHeight; 

     if (droid.x - decentreX < curR & droid.x + decentreX > curL) { 
      if (droid.y - decentreY < curD & droid.y + decentreY > curU) { 
     myImage.setBounds(curL,curU,curR,curD); 
     myImage.draw(canvas); 
      } 
     } 
     } 
     } 

     droid.draw(canvas); 
     butt.draw(canvas); 
     butt1.draw(canvas); 
     butt2.draw(canvas); 
     butt3.draw(canvas); 
     buttz.draw(canvas); 
     buttz1.draw(canvas); 
     buttz2.draw(canvas); 
     buttz3.draw(canvas); 
     buttx.draw(canvas); 
    } 

但它沒有奏效。所有不在這種方法中並不重要。幫我! :)

+1

「沒有工作」非常含糊。你遇到了什麼問題? – kostja 2012-01-03 18:49:03

+0

我如何移動地圖? – Liukas 2012-01-03 19:06:04

+0

哦,問題是我沒有得到任何錯誤,但地圖只是移動1px並停下來。 – Liukas 2012-01-03 19:08:19

回答

1

似乎你正在改變lcal變量curLcurU。每次呼叫都會從rowBase重新計算,限制移動到檢查droid.touched時使用的1-增量。

您應該通過更改rowBase變量來更改切片的全局位置。

+0

它的工作原理,謝謝:) – Liukas 2012-01-03 19:45:00

+0

另一個簡短的問題:有沒有辦法獲得數組中的列和行數? – Liukas 2012-01-03 19:53:03

+0

@Liukas不客氣。嘗試'board.length'和'board [0] .length' – kostja 2012-01-03 20:13:05