我得到了超级马里奥级别1(1563x224)的图像:java - Libgdx:FitViewPort无法正常工作-LMLPHP
但是,当您玩游戏时,并没有看到所有的关卡。
这些是我的价值观:

// 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(工作):
java - Libgdx:FitViewPort无法正常工作-LMLPHP

查看端口大小:300x100(不起作用):
java - Libgdx:FitViewPort无法正常工作-LMLPHP

我究竟做错了什么?

最佳答案

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选择来管理Camera的viewportWidth和viewportHeight。

每当调整大小事件发生时,计算视口参数并更新摄影机。

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

10-02 09:13