Libgdx:FitViewPort无法正常工作

时间:2017-05-26 23:09:19

标签: java camera libgdx viewport

我得到了超级马里奥级别1(1563x224)的图像:super mario level1 但是当你玩游戏时,你并没有看到所有级别。
这些是我的价值观:

// 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;

这是我游戏构造函数的一部分:

    _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

这是我的渲染方法:

@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();
}

由于某种原因,当我尝试从我的图片中更改我的VIEWPORT_HEIGHTVIEWPORT_WIDTH时,它似乎只适用于这些口粮:

查看端口大小:300x224(正常工作): 300x224 image working

查看端口大小:300x100(不工作): 300x100 image not working

我做错了什么?

1 个答案:

答案 0 :(得分:1)

FitViewport始终保持虚拟屏幕大小(虚拟视口)的宽高比,同时尽可能地缩放以适应屏幕。

viewportWidth的构造函数中传递viewportHeightOrthographicCamera,而不是.tmx总维度。

_gamecam = new OrthographicCamera();
_gamecam.setToOrtho(false,ZombieGame.VIEWPORT_WIDTH,ZombieGame.VIEWPORT_HEIGHT);
_viewport = new FitViewport(ZombieGame.VIEWPORT_WIDTH, ZombieGame.VIEWPORT_HEIGHT, _gamecam);

我们使用视口,因为它根据ViewPort选项管理相机的viewportWidth和viewportHeight。

计算视口参数并在每次调整大小事件时更新相机。

@Override
public void resize(int width, int height) {
    _viewport.update(width,height,true); 
}