2013-02-08 44 views
0

我正在使用文本文件來存儲地圖,但加載它們會給我帶來一點麻煩。物體被擠在一起,我不知道爲什麼。這裏是「地圖」文件(level_1_1.map):從數組中加載地圖(對於遊戲)的問題

"level_1_1.png", ?600:400?; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; 
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1; 
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0; 
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0; 

這裏是我加載的類:

package org.awesome.Models; 



public class Map{ 
int playerWidth = 50; 
int playerHeight = 50; 
int platformWidth = 20; 
int platformHeight = 20; 

int playerX, playerY; 
List<Platform> platforms = new ArrayList<Platform>(); 
Bitmap bg; 
int backgroundX, backgroundY; 

private Context context; 
private SurfaceView game; 
public Map(Context context, SurfaceView game){ 
    this.context = context; 
    this.game = game; 
} 

public void update(){ 

} 

public void draw(Canvas canvas){ 

    for(Platform p : platforms) 
     p.draw(canvas); 
} 

public boolean loadMap(String filename){ 
    //load the file into a string 
    byte[] buffer = null; 
    try { 
     AssetManager am = context.getAssets(); 
     InputStream fis = am.open(filename); 
     buffer = new byte[fis.available()]; 
     fis.read(buffer, 0, fis.available()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    String s = new String(buffer); 
    //seperate the map image, the dimensions and the mapping itself 
    s = s.substring(s.indexOf("\"") + 1); 
    String bgname = s.substring(0, s.indexOf("\""));  
    bg = BitmapFactory.decodeFile(bgname); 
    s = s.substring(s.indexOf(",") + 1); 
    s = s.substring(s.indexOf("?") + 1); 
    String dimensions = s.substring(0, s.indexOf("?")); 
    //actual dimensions 
    int width = Integer.parseInt(dimensions.substring(0, s.indexOf(":"))); 
    int height = Integer.parseInt(dimensions.substring(s.indexOf(":") + 1)); 

    s = s.substring(s.indexOf(";") + 1); 
    String[] mapRows = s.split(";"); 
    //the actual mapping of things 
    String[][] map = new String[mapRows.length][]; 
    for(int i = 0; i < mapRows.length; i++){ 
     map[i] = mapRows[i].split(","); 
    } 

    //now create the stuff 
    for(int r = 0; r < map.length; r++){ 
     for(int c = 0; c < map[r].length; c++){ 
      map[r][c] = map[r][c].trim(); 
      int currentDigit = Integer.parseInt(map[r][c]); 
      int x = c * 10; 
      int y = r * 10; 

      //Will create the object at the top left corner of the !!RECTANGULAR!! map representation. 
      if(currentDigit == 0){ 

      }else if(currentDigit == 1 && isTopLeft(1, r, c, map)){ 
       Bitmap texture = BitmapFactory.decodeResource(game.getResources(), org.awesome.AndroidGame.R.drawable.brown_block_reg); 
       Log.w("making platform", "" + x + ", " + y); 
       platforms.add(new Platform(x, y, texture)); 
      }else if(currentDigit == 2 && isTopLeft(2, r, c, map)){ 
       playerX = x; 
       playerY = y; 
      }else if(currentDigit == 3){ 

      }else if(currentDigit == 4){ 

      }else if(currentDigit == 5){ 

      }else if(currentDigit == 6){ 

      }else if(currentDigit == 7){ 

      }else if(currentDigit == 8){ 

      }else if(currentDigit == 9){ 

      } 
     } 
    } 

    return true; 
} 

private boolean isTopLeft(int item, int r, int c, String[][] map){ 
    //width and height measured in # of map "steps" (10px ea) 
    int width = 1; 
    int height = 1; 

    if(item == 0){ 

    }else if(item == 1){ 
     width = platformWidth/10; 
     height = platformHeight/10; 
    }else if(item == 2){ 
     width = playerWidth/10; 
     height = playerHeight/10; 
    }else if(item == 3){ 

    }else if(item == 4){ 

    }else if(item == 5){ 

    }else if(item == 6){ 

    }else if(item == 7){ 

    }else if(item == 8){ 

    }else if(item == 9){ 

    } 

    //counts backwards and upwards to see how many blocks of the same type preceed it. 
    int curR = r - 1; 
    int prevWidthCount = 0; 
    int prevHeightCount = 0; 
    while(curR >= 0 && map[curR][c].equals("" + item)){ 
     curR -= 1; 
     prevWidthCount += 1; 
    } 
    int curC = c - 1; 
    while(curC >= 0 && map[r][curC].equals("" + item)){ 
     curC -= 1; 
     prevHeightCount += 1; 
    } 


    if(prevWidthCount % width == 0 && prevHeightCount % height == 0){ 
     return true; 
    } 

    return false; 

} 

public int countObjectsLeft(String[][] map, int r, int c){ 
    int counter = 0; 
    while(c >= 0){ 
     if(isTopLeft(Integer.parseInt(map[r][c]), r, c, map)) 
      counter++; 
     c -= 1; 
    } 
    return counter; 
} 
public int countObjectsUp(String[][] map, int r, int c){ 
    int counter = 0; 
    while(r >= 0){ 
     if(isTopLeft(Integer.parseInt(map[r][c]), r, c, map)) 
      counter++; 
     r -= 1; 
    } 
    return counter; 
} 

} 

下面是結果(管道字符,每個塊結束),球是玩家(目前沒有從文件加載)。 enter image description here

我被困在這,看起來塊應該適合(0,0)到(19,19)和(20,20)到(39,39)。任何幫助,將不勝感激:d:d

編輯1:

這裏是全平臺類:

package org.awesome.Models; 


public class Platform { 
private int x, y; 
private Bitmap texture; 
public Platform(int x, int y, Bitmap texture){ 
    this.x = x; 
    this.y = y; 
    this.texture = texture; 

} 

public void draw(Canvas canvas) { 
    Paint paint = new Paint(); 
    paint.setColor(Color.WHITE); 
     canvas.drawBitmap(texture, x, y, null); 
     canvas.drawText("|", x + texture.getWidth(), y - 2, paint); 
    } 

public int getX(){ 
    return x; 
} 

public int getY(){ 
    return y; 
} 

public Bitmap getBitmap(){ 
    return texture; 
} 
} 
+0

我的第一個猜測是你在'Platform'中做了一些事情,作爲我們需要看到的繪圖函數的一部分。 – Grambot 2013-02-08 19:33:02

回答

1

在你的代碼中,我看到你正在使用dimmensions以像素爲單位,而BitmapFactory.decodeResource進行位圖,以適應當前設備的屏幕密度scalling,這couses繪製的圖像,以更大的是應該的。如果你想使用像素,那麼你應該使用選項(設置inScaled爲false應該足夠了)版本decodeResource禁用scalling:

decodeResource(Resources res, int id, BitmapFactory.Options opts) 

或者把你的可繪製在

drawable-nodpi 

文件夾,有什麼會阻止scalling在加載期間

+0

的inScaled =假沒有出於某種原因。我的項目中沒有drawable-nodpi,我可以創建它嗎? – ceptno 2013-02-08 20:07:58

+0

是創建它,我想你的位圖drawBitmap期間還是縮減規模,你的畫布是其他密度。我認爲你應該遠離使用像素昏暗 - 你的遊戲屏幕在高密度設備上看起來會很小。 – marcinj 2013-02-08 20:16:41

+0

的計劃是讓地圖真正的大和有口。有沒有其他的方法來做到這一點(像素以外)?我對這些東西不是很有經驗,並且很想學習。 – ceptno 2013-02-08 22:00:05