Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing a tile based map
    text
    copied!<p>This script draws the controls, hero, surface and the map:</p> <pre><code>public void render(Canvas canvas) { canvas.drawColor(Color.TRANSPARENT); Drawable myImage; int tileWidth = 50; int tileHeight = 50; int rowBaseX = 0; int rowBaseY = 0; int[][] board = new int[][] { {0,0,0,0,0,0,2,0,0,0,}, {0,0,0,0,0,2,2,0,0,0,}, {0,0,0,0,0,2,0,0,0,0,}, {0,0,0,0,0,2,0,0,0,0,}, {0,0,0,2,2,2,0,0,0,0,}, {0,0,0,2,0,0,0,0,0,0,}, {0,0,0,2,0,0,0,0,0,0,}, {0,0,2,2,0,0,0,0,0,0,}, {0,0,2,0,0,0,0,0,0,0,}, {0,0,2,0,0,0,0,0,0,0,} }; int mapWidth = 10; int mapHeight = 10; for (int row = 0; row &lt; mapHeight; row++) { for (int col = 0; col &lt; 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 = rowBaseX + (col * tileWidth); int curU = rowBaseY + (row * tileHeight); int curR = curL + tileWidth; int curD = curU + tileHeight; 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); } </code></pre> <p>There is a hero, which has to be redrawn when player moves him with the controls, and all other drawables also has to be redrawn. The problem is that drawing a map is a long process, so the bigger map i create, the slower hero moves, because every tile of the map has to be painted. Is there a way to put all the tiles to a one bitmap in other method and draw that one bitmap in the canvas method?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload