I got image of super mario level1 (1563x224):
But when you play the game you are not seeing all the level.
These are my values:
// level1.png image size: 1563x224
// viewPort size: 300x224
public static final float LEVEL1_WIDTH = 1563f;
public static final float LEVEL1_HEIGHT = 224f;
public static final float VIEWPORT_WIDTH = 300f;
public static final float VIEWPORT_HEIGHT = 224f;
This is part of my game constructor:
_gamecam = new OrthographicCamera(ZombieGame.LEVEL1_WIDTH, ZombieGame.LEVEL1_HEIGHT); // set the world size
_viewport = new FitViewport(ZombieGame.VIEWPORT_WIDTH, ZombieGame.VIEWPORT_HEIGHT, _gamecam); // set the viewport size
_gamecam.position.set(ZombieGame.VIEWPORT_WIDTH / 2f, ZombieGame.VIEWPORT_HEIGHT / 2f, 0); // set the camera to be in the middle of the viewport
_background = new Texture("level1.png"); // load level1 (background) image
This is my render method:
@Override
public void render(float delta) {
_gamecam.update();
Gdx.gl.glClearColor(0,1,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
_game.batch.setProjectionMatrix(_gamecam.combined);
_game.batch.begin();
_game.batch.draw(_background, 0, 0, ZombieGame.LEVEL1_WIDTH, ZombieGame.LEVEL1_HEIGHT);
_game.batch.end();
}
For some reason it seemed it's only working in these rations, when I try to change my VIEWPORT_HEIGHT
or VIEWPORT_WIDTH
it's shows out of my image:
view port size: 300x224
(working):
view port size: 300x100
(not working):
What am I doing wrong?
FitViewport always maintain the aspect ratio of the virtual screen size (virtual viewport), while scaling it as much as possible to fit the screen.
Pass viewportWidth
and viewportHeight
in constructor of OrthographicCamera
not your .tmx
total dimension.
_gamecam = new OrthographicCamera();
_gamecam.setToOrtho(false,ZombieGame.VIEWPORT_WIDTH,ZombieGame.VIEWPORT_HEIGHT);
_viewport = new FitViewport(ZombieGame.VIEWPORT_WIDTH, ZombieGame.VIEWPORT_HEIGHT, _gamecam);
We use viewport because it manages a Camera's viewportWidth and viewportHeight according to our ViewPort choice.
Calculate the viewport parameters and update the camera whenever resize event occurs.
@Override
public void resize(int width, int height) {
_viewport.update(width,height,true);
}