Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid dip vis pixels. Having terrible trouble with my 2d tilemap?
    text
    copied!<p>I've searched/read all of the dip to pixel translations, but I'm feeling a little overwhelmed by it all, I'm sure it's incredibly easy to grasp, but I'm quite old school :( So I'm pleading for some help on the matter, if anyone's got the time.</p> <p>I've got an android game using a SurfaceView, which draws a 2d tilemap to the screen for a puzzle game. I've stupidly hardcoded all values for my device, just to get it up and running. Now it's pretty much 100% complete, I went out and bought a Samsung Galaxy tab, and it looks like poop! Can anyone shed some light on how to get the following code to work in dip/dp, rather than pixels please?</p> <p>TIA!</p> <pre><code> public static int MAX_WIDTH = 320; // width of map area public static int MAX_HEIGHT = 480; // height of map area public static final int TILE_SIZE_2 = 32; // tile size dimensions </code></pre> <p>I then set my map area to a pre-defined area</p> <pre><code> map_area = new int[MAX_WIDTH/TILE_SIZE][MAX_HEIGHT/TILE_SIZE]; </code></pre> <p>Here is how I setup the levels themselves (you'll see what I mean when I'm old school) - It's a shambles, I know! I do have time to waste though!</p> <pre><code> public void mapStringToMap(String mapString){ // clear the map first int i= 0; rocks = new CTile[mapString.length()]; try { // now add the rock types to the map array for(int x = 0; x&lt;MAX_WIDTH/TILE_SIZE; x++){ for(int y =0; y&lt;MAX_HEIGHT/TILE_SIZE; y++){ rocks[i] = new CTile(); setMapData(x, y, Integer.parseInt(String.valueOf(mapString.charAt(i)))); rocks[i].setRockType(Integer.parseInt(String.valueOf(mapString.charAt(i)))); rocks[i].setX(x*TILE_SIZE); rocks[i].setY(y*TILE_SIZE); rocks[i].setRockId(i); if(Integer.parseInt(String.valueOf(mapString.charAt(i))) == GOLD_TILE){ rocks[i].setRockBitmap(goldTile); // increase the number of rocks for this level level_rock_count+=3; }else if(Integer.parseInt(String.valueOf(mapString.charAt(i))) == SILVER_TILE){ rocks[i].setRockBitmap(silverTile); // increase the number of rocks for this level level_rock_count+=2; }else if(Integer.parseInt(String.valueOf(mapString.charAt(i))) == COPPER_TILE){ rocks[i].setRockBitmap(copperTile); // increase the number of rocks for this level level_rock_count++; }else if(Integer.parseInt(String.valueOf(mapString.charAt(i))) == START_TILE){ dave.setX(x*TILE_SIZE); dave.setY(y*TILE_SIZE); }else if(Integer.parseInt(String.valueOf(mapString.charAt(i))) == DIAMOND_TILE_1){ rocks[i].setRockBitmap(diamondTile1); // increase the number of rocks for this level by 4! level_rock_count += 4; } // move on i++; } } } catch(ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } } </code></pre> <p>As a result, it works wonderfully on my device, but I'm trying to cover as many handsets as I can, obviously. The drawing of the maps is pretty much identical to above, the whole grid values x/y * TILE_SIZE etc. </p> <p>I'm sure I'm going to kick myself, but I just can't wrap my head around anything I've read.</p> <p>Cheers.</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