简体   繁体   中英

Problems drawing a Texture on a TileMap in java

I have problems to draw an image ( Texture ) on a TiledMap. When I try to call batch.draw(mytexture,x,y) the only image displayed is the map below; 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. I create a map where I put a String as a key and a Texture corresponding to the image I want to display

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 {

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 ? When calling the constructor of OrthogonalTiledMapRenderer that only takes a TmxTileMap as parameter the unit scale is defaulted to 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 . So if the tiles for your map are 64 by 64 pixels change

renderer = new OrthogonalTiledMapRenderer(map);

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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