0

I'm using libgdx in my 2D platformer game project for college task. For now I'm confused with loading tiledmap in libgdx. I have a big tmx map with size of 11400x1500 pixels and 30x30 tile size. Also an image with same pixel size which I load it in image layer. I try to load it but libgdx shows nothing.
My question is how to load single big tmx map or should I divide the map into several segment/section? Because I've tried other small tmx (3000x1500). If the tmx is divided then how to load them as one stage?

public class Layarloh implements Screen{
private JumatUtama game;
private TiledMap map;
private TmxMapLoader mapLoader;
private OrthographicCamera kamera;
private OrthogonalTiledMapRenderer mapRenderer;
private Viewport viewport;

public Layarloh(JumatUtama utama) {
    float l=Gdx.graphics.getWidth(),t=Gdx.graphics.getHeight();
    this.game = utama;

    loadstage();
    kamera = new OrthographicCamera();
    kamera.setToOrtho(false,l,t);
    kamera.update();
    viewport = new FitViewport(l/3,t/3,kamera);

}
public void loadstage(){
    mapLoader = new TmxMapLoader();
    map = mapLoader.load("stage/s11.tmx");
    mapRenderer = new OrthogonalTiledMapRenderer(map);

}
public void stik(float dt){
    if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY))
        kamera.position.x +=100*dt;

}
@Override
public void render(float delta) {
    stik(delta);
    kamera.position.set(550,550,0);
    kamera.update();
    mapRenderer.setView(kamera);
    mapRenderer.render();
}
@Override
public void resize(int width, int height) {
    kamera.viewportWidth = viewport.getScreenWidth();
    kamera.viewportHeight = viewport.getScreenHeight();
    kamera.update();
}


Unitscale does nothing, the code above works on smaller size of tmx map.

EDIT

Code above isn't working, and the below is working, notice the maploader is loading different tmx file, above code trying to load 11400x1500 pixel with 30x30 tile size, while code below trying to load 3800x1500 with 30x30 tile size, but I try to use the code below to load bigger tmx, its not working,

public class Layarloh implements Screen{
private JumatUtama game;
private TiledMap map;
private TmxMapLoader mapLoader;
private OrthographicCamera kamera;
private OrthogonalTiledMapRenderer mapRenderer;

public Layarloh(JumatUtama utama) {
    float l=Gdx.graphics.getWidth(),t=Gdx.graphics.getHeight();
    this.game = utama;

    loadstage();
    kamera = new OrthographicCamera();
    kamera.setToOrtho(false,l,t);
    kamera.position.set(550,550,0);
    kamera.update();

}

public void loadstage(){
    mapLoader = new TmxMapLoader();
    map = mapLoader.load("stage/segmen1.tmx");
    mapRenderer = new OrthogonalTiledMapRenderer(map);

}
public void stik(float dt){
    if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY))
        kamera.position.x +=100*dt;
}

@Override
public void render(float delta) {
    stik(delta);
    kamera.update();
    mapRenderer.setView(kamera);
    mapRenderer.render();
}
pras
  • 97
  • 1
  • 14
  • Does it show anything on the console? Try enabling the logger. – Sneh Jan 01 '16 at 03:08
  • kamera.position.x +=100*dt; Just try disabling it for now and see what happens. – Sneh Jan 01 '16 at 03:11
  • kamera.position.set(550,550,0); Can you also try using 0, 0, 0 here rather than 550, 550, 0? – Sneh Jan 01 '16 at 03:20
  • kamera.position.x is just for testing scrollable background when I press any key, and it works fine, kamera.position.set is to set the camera so I can see picture, here's my stage1.png [link](https://app.box.com/s/b9zjs43w6eeo3lmxa6rad5gnapcz5n2p) its size is 11400x1500 pixels – pras Jan 01 '16 at 05:37
  • Have you tried with smaller map just to check it works or not? – Sneh Jan 01 '16 at 11:12
  • load the smaller map is works, but thats make me to divide the map, and if I divide the map, how I can load all of them as one? – pras Jan 01 '16 at 11:40
  • 1
    Rather than loading all at once which eats up a lot of resources, load the new map when you reach corner of a map. You can get that using map properties that where the end of map is. As soon as player reaches end, load a new map. – Sneh Jan 01 '16 at 14:36

1 Answers1

1

Try this code and this should fix it.

@Override
public void render(float delta) {
    kamera.position.set(x, y, z);
    kamera.update();
    mapRenderer.setView(kamera);
    mapRenderer.render();
}

@Override
public void resize(int width, int height) {
    kamera.viewportWidth = width;
    kamera.viewportHeight = height;
    kamera.update();
}

EDIT In the original code, OP was missing kamera.update() statement. And also the resize method. That is something which causes trouble when displaying the tiled map properly as you need to update the camera at every iteration of render.

Sneh
  • 3,527
  • 2
  • 19
  • 37
  • I've updated the question with including your code but still doesn't work, the code I've updated is my `PlayScreen` _class_ – pras Jan 01 '16 at 03:10
  • Why should it fix it? This is only the less useful part of an answer. – msw Jan 01 '16 at 03:15
  • Because the solution was missing camera.update() method, OP edited his question now. – Sneh Jan 01 '16 at 03:16
  • 1
    @msw I have edited my solution and given an explanation. :) – Sneh Jan 01 '16 at 03:23