This is not my first game and I thought that I understood the way the camera was working. It is, however, my first time making a game using TileMaps (created using Tiled in this case). I did create a prototype before which works perfectly, but, for some reason, that code doesn’t work in my new project. As far as I can see, the only difference is that Tiled seems to force turning tilesets into a .tsx file whereas they were .png files before. I’m using libGDX.
So, here’s my problem - using the following code:
public class BaseScreen extends ScreenAdapter {
public static final String TAG = "BaseScreen";
protected Game game;
protected OrthographicCamera gameCamera;
protected Viewport gameViewport;
public BaseScreen(Game game) {
super();
this.game = game;
gameCamera = new OrthographicCamera();
gameCamera.setToOrtho(false, Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);
gameCamera.update();
gameViewport = new StretchViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT
, gameCamera);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
public class GameScreen extends BaseScreen {
private final String TAG = "GameScreen";
private OrthogonalTiledMapRenderer mapRenderer;
public GameScreen(Game game) {
super(game);
TiledMap map = Assets.getInstance().getMap();
gameCamera.update();
mapRenderer = new OrthogonalTiledMapRenderer(map, 1 / 32f);
Gdx.input.setInputProcessor(new TigerGameScreenInputHandler());
}
@Override
public void render(float delta) {
super.render(delta);
gameCamera.update();
mapRenderer.setView(gameCamera);
mapRenderer.render();
}
private class TigerGameScreenInputHandler extends InputAdapter {
@Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.ESCAPE) {
GameScreen.this.dispose();
Gdx.app.exit();
} else if (keycode == Input.Keys.LEFT) {
gameCamera.translate(+1, 0);
} else if (keycode == Input.Keys.RIGHT) {
gameCamera.translate(-1, 0);
} else if (keycode == Input.Keys.UP) {
gameCamera.translate(0, +1);
} else if (keycode == Input.Keys.DOWN) {
gameCamera.translate(0, -1);
}
gameCamera.update();
return false;
}
}
}
I get a map rendering with its bottom left corner in the middle of the screen - i.e. width / 2 x height /2 and the moving around of the camera using the arrow keys works as expected. I want the map to have it’s bottom left corner at 0,0.
If, however, I insert the following line of code into the render method after the call to super.render():
gameCamera.setToOrtho(false, Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);
Then the map is positioned at 0,0, but the keys make no difference. I’m not massively surprised at how in the second instance the keys don’t work since I’m assuming that the call to setToOrtho() repositions the camera at 0, but I can’t understand why the setToOrtho() is required in render() in order to get the map to render at 0,0
Similarly, if I run the original code without the line
mapRenderer.setView(gameCamera);
in the render() method then I get the same results as in the second instance above.
I’m assuming that I’m just forgetting or missing something stupid, but I’ve compared to the previously working code in my prototype and to online tutorials and I can’t see what I’m doing wrong.
Hope that all made sense!
Please put me out of my misery! Thanks in advance.