0
\$\begingroup\$

I'm trying to study a book to learn LibGDX game engine but I have a problem with rendering tiled maps. I think I wrote the same code that is in the book but I couldn't get the same result.

It's a simple game with a character and a map. When I rendered my character and background, there was no problem.

It's looking like this:

enter image description here

But after I add my tmx map, the screen shows just some part of the game and no map.

I don't know how to fix this and I'm really confused.

enter image description here

The code below are my GameManager and ScreenManager classes. If possible, please figure out what I did wrong.

public class GameManager {

 static TiledMap map;
 public static TiledMapRenderer renderer; /////

 //region paddle
 static TextureRegion leftPaddleTexture;
 static TextureRegion rightPaddleTexture;
 static Sprite leftPaddleSprite;
 static Sprite rightPaddleSprite;
 public static final float PADDLE_RESIZE_FACTOR = 700 f;
 public static final float PADDLE_ALPHA = 0.25 f;
 public static final float PADDLE_HORIZ_POS_FACTOR = 0.02 f;
 public static final float PADDLE_VERT_POSITION_FACTOR = 0.01 f;
 //endregion

 static AssetManager assetManager;
 static TextureAtlas texturePack;

 static Bob bob;

 static TextureRegion bobSpriteSheet;

 public static Sprite backgroundSprite;
 public static Texture backgroundTexture;

 public static final float BOB_RESIZE_FACTOR = 400 f;

 public static void loadAssets() {
  assetManager.load(GameConstants.backgroundImage, Texture.class);
  assetManager.load(GameConstants.texturePack, TextureAtlas.class);


  assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));


  assetManager.load(GameConstants.level1, TiledMap.class);


  assetManager.finishLoading();
 }

 public static void initialize(float width, float height) {



  assetManager = new AssetManager();
  loadAssets();


  map = assetManager.get(GameConstants.level1);
  renderer = new OrthogonalTiledMapRenderer(map, GameConstants.unitScale);

  GameScreen.camera.setToOrtho(false, 35, 20);
  GameScreen.camera.update();


  renderer.setView(GameScreen.camera);

  texturePack = assetManager.get(GameConstants.texturePack);

  initializeLeftPaddle(width, height);
  initializeRightPaddle(width, height);

  bob = new Bob();
  bobSpriteSheet = texturePack.findRegion(GameConstants.bobSpriteSheet);
  bob.initialize(width, height, bobSpriteSheet);


  bob.bobSprite = new Sprite(bobSpriteSheet);

  //set the size of the bob
  bob.bobSprite.setSize((walkSheet.getRegionWidth() / ANIMATION_FRAME_SIZE) * (width / BOB_RESIZE_FACTOR),
   walkSheet.getRegionHeight() * (width / BOB_RESIZE_FACTOR));

  bob.bobSprite.setPosition(width / 2 f, 0);

  backgroundTexture = assetManager.get(GameConstants.backgroundImage);
  backgroundSprite = new Sprite(backgroundTexture);

  backgroundSprite.setSize(width, height);

 }

 public static void renderGame(SpriteBatch batch) {
  backgroundSprite.draw(batch);
  bob.update();
  bob.render(batch);
  leftPaddleSprite.draw(batch);
  rightPaddleSprite.draw(batch);

 }

 public static void dispose() {
  assetManager.unload(GameConstants.backgroundImage);
  assetManager.clear();
 }

 public static void initializeLeftPaddle(float width, float height) {
  leftPaddleTexture = texturePack.findRegion(GameConstants.leftPaddleImage);
  leftPaddleSprite = new Sprite(leftPaddleTexture);

  leftPaddleSprite.setSize(leftPaddleSprite.getWidth() * width / PADDLE_RESIZE_FACTOR,
   leftPaddleSprite.getHeight() * width / PADDLE_RESIZE_FACTOR);

  leftPaddleSprite.setPosition(width * PADDLE_HORIZ_POS_FACTOR, height * PADDLE_VERT_POSITION_FACTOR);

  leftPaddleSprite.setAlpha(PADDLE_ALPHA);
 }

 public static void initializeRightPaddle(float width, float height) {
  rightPaddleTexture = texturePack.findRegion(GameConstants.rightPaddleImage);
  rightPaddleSprite = new Sprite(rightPaddleTexture);

  rightPaddleSprite.setSize(rightPaddleSprite.getWidth() * width / PADDLE_RESIZE_FACTOR,
   rightPaddleSprite.getHeight() * width / PADDLE_RESIZE_FACTOR);

  rightPaddleSprite.setPosition(leftPaddleSprite.getX() + leftPaddleSprite.getWidth() + width * PADDLE_HORIZ_POS_FACTOR,
   height * PADDLE_VERT_POSITION_FACTOR);

  rightPaddleSprite.setAlpha(PADDLE_ALPHA);
 }
}

public class GameScreen implements Screen {

 MainGame game;
 SpriteBatch batch;
 public static OrthographicCamera camera;

 public GameScreen(MainGame game) {
  this.game = game;
  float height = Gdx.graphics.getHeight();
  float width = Gdx.graphics.getWidth();

  camera = new OrthographicCamera(width, height);
  camera.setToOrtho(false);

  batch = new SpriteBatch();

  GameManager.initialize(width, height);

  Gdx.input.setInputProcessor(new InputManager(camera));

 }

 @Override
 public void show() {

 }

 @Override
 public void render(float delta) {
  Gdx.gl.glClearColor(1, 1, 1, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


  batch.setProjectionMatrix(camera.combined);


  GameManager.renderer.render();


  batch.begin();
  GameManager.renderGame(batch);
  batch.end();





 }

 @Override
 public void resize(int width, int height) {

 }

 @Override
 public void pause() {

 }

 @Override
 public void resume() {

 }

 @Override
 public void hide() {

 }

 @Override
 public void dispose() {
  batch.dispose();
  GameManager.dispose();

 }
}

I also searched on site to find same problem but I couldn't. I hope the community can help and Thank You if you can.

\$\endgroup\$
  • \$\begingroup\$ First thing: you render your map before the background. Are you sure this is what you wanna do? if you render the background sprite after the map, it mays hide your map. \$\endgroup\$ – nathan Feb 23 '16 at 11:42
  • \$\begingroup\$ public void render(float delta) { Gdx.gl.glClearColor(1,1,1,1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); GameManager.renderer.setView(camera); GameManager.renderer.render(); batch.begin(); GameManager.renderGame(batch); batch.end(); } i change code to this but result was the same :/ \$\endgroup\$ – Ali Lei Feb 23 '16 at 14:32
1
\$\begingroup\$

The following code is the problem:

GameScreen.camera.setToOrtho(false, 35,20);

This basically says - make the screen show only 35 across and 20 vertically of the area. If you use larger numbers, you will see more (zoom out).

\$\endgroup\$
  • \$\begingroup\$ yes, i changed it but the main problem is i can not see the tiled map.. There are only background and character. \$\endgroup\$ – Ali Lei Feb 25 '16 at 18:31
0
\$\begingroup\$

Problem is in this line of code:

renderer = new OrthogonalTiledMapRenderer(map, GameConstants.unitScale);

// it means load the map, set the unit scale to 1/16 (1 unit == 16 pixels)

 GameScreen.camera.setToOrtho(false, 35,20);
    GameScreen.camera.update();

// create an orthographic camera, shows us 35x20 units of the world

what renderer does, it shows you in pixels so it is showing you that part with that scale 16 pixel is tile width/height in your map and 35,20 is how much map size you want to render at a time.Hope this helps

Here is link to source code for simple platformer:

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.