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,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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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;
}

}

这是结果(管道字符是每个块的结束位置),球是玩家(当前未从文件加载)。在此处输入图像描述

我坚持这一点,似乎块应该适合 (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;
}
}
4

1 回答 1

1

在您的代码中,我看到您正在使用以像素为单位的尺寸,而 BitmapFactory.decodeResource 执行位图的缩放以适应当前设备的屏幕密度,这会使绘制的图像更大。如果要使用像素,则应使用选项禁用 decodeResource 中的缩放(将 inScaled 设置为 false 就足够了)版本:

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

或者把你的drawables放进去

drawable-nodpi

文件夹,什么会在加载过程中防止缩放

于 2013-02-08T19:50:10.890 回答