简体   繁体   English

在Java中的TileMap上绘制纹理时遇到问题

[英]Problems drawing a Texture on a TileMap in java

I have problems to draw an image ( Texture ) on a TiledMap. 我在TiledMap上绘制图像(Texture)时遇到问题。 When I try to call batch.draw(mytexture,x,y) the only image displayed is the map below; 当我尝试调用batch.draw(mytexture,x,y)时,显示的唯一图像是下面的地图; I tried to search on web a feasible solution, but i have not solved the problem yet.. 我试图在网络上搜索可行的解决方案,但尚未解决问题。

i 一世

Here's my code 这是我的代码

public class GameTest implements ApplicationListener{
private Player player;

private Batch batch;
private MyTexture texture;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private TiledMap map;

public GameTest() {

//init camera and player
}
@Override
public void create() {      
    background = new Background();
    batch = new SpriteBatch();
    texture = new MyTexture();

    map = new TmxMapLoader().load(Asset.FIRST_LEVEL);
    renderer = new OrthogonalTiledMapRenderer(map);
    camera.setToOrtho(false, 1280,512);
    renderer.setView(camera);
    camera.update();
}
@Override
public void render() {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    background.update(Gdx.graphics.getDeltaTime());
    batch.begin();

    if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)){
        game.movePlayer('r', 1);
        camera.position.x += 5;
    }
    camera.update();        
    renderer.setView(camera);
    renderer.render();

//      batch.setProjectionMatrix(camera.combined);
    batch.draw(texture.getTexture("100"), (player.getPosition().y) * 64, ((7 
 - player.getPosition().x) * 64));
    batch.end();
}

}

Here's MyTexture class. 这是MyTexture类。 I create a map where I put a String as a key and a Texture corresponding to the image I want to display 我创建了一个地图,在该地图中,我将String作为键,将Texture对应于要显示的图像

public MyTexture() {

    ....
        map.put("100",new Texture(Gdx.files.internal(Asset.PLAYER)));

}   
public static final Texture getTexture(String key){
    return map.get(key);
}

And here my Asset class, where I create only static field for imgaes path public class Asset { 这是我的Asset类,在这里我仅为图像创建静态字段path public class Asset {

public static Map<String, String> map = new HashMap<String,String>();

public static final String FIRST_LEVEL = "levels/firstLevel.tmx";
public static String BACKGROUND = "asset/Background.png";
public static String PLAYER = "asset/Player.png";
...

}

Are you use you want to draw with a unit-scale of 1.0 ? 您是否要使用1.0单位比例绘制? When calling the constructor of OrthogonalTiledMapRenderer that only takes a TmxTileMap as parameter the unit scale is defaulted to 1.0 . 调用仅以TmxTileMap作为参数的OrthogonalTiledMapRenderer构造函数时,单位比例默认为1.0 That means that a single tile takes up the as many world-units as there are pixels in a tile. 这意味着单个图块占用的世界单位与图块中像素的数量一样多。

Try calling it with 1.0f / your_tile_size_in_pixels . 尝试使用1.0f / your_tile_size_in_pixels调用它。 So if the tiles for your map are 64 by 64 pixels change 因此,如果地图的图块是64 x 64像素,

renderer = new OrthogonalTiledMapRenderer(map);

to

renderer = new OrthogonalTiledMapRenderer(map, 1.0f / 64.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM