适用于不同阶段/相机的Fitviewport LIBGDX

时间:2018-12-07 12:32:45

标签: android libgdx resize viewport

因此,我已经阅读了很多关于fitviewport的内容,并且无法在我的实际项目中使用它。我有不同的阶段,一台用于菜单/界面的相机,另一台用于平铺地图的相机。

我的平铺地图每个渲染一次渲染一次,但是trees图层渲染两次(我想这样)。而且无法使其正确安装。

BasicScreen:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(.25f, .25f, .4f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    cam.update();
    stage.getViewport().apply();
    game.getBatch().setProjectionMatrix(cam.combined);

    //stage.act(delta);
    //stage.draw();
}


@Override
public void show() {
    if (cam == null) {
        cam = new OrthographicCamera();
        cam.setToOrtho(false, Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
    }

    if (viewport == null) {
        viewport = new FitViewport(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT, cam);
        viewport.apply();
    }


    if (stage == null) {
        stage = new Stage(viewport, game.getBatch());
    }

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

MenuScreen:

@Override
public void setUpInterface(Table table) {
    skinStage = new SkinsStage(game, viewport, listener);
    infoStage = new InfoStage(game, viewport, listener);
    onlineStage = new OnlineStage(game, viewport, listener);
    settingsStage = new SettingsStage(game, viewport, listener, nightListener);
    statisticsStage = new StatisticsStage(game, viewport, listener);


if(map == null) {
        map = new TownMap(game, 1f);
        //camMap = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
        camMap = new OrthographicCamera(viewport.getWorldWidth(), viewport.getWorldHeight());
        camMap.setToOrtho(false, Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
        camMap.position.x += 10*Constants.PixelsPerMeter;
        camMap.position.y += 10*Constants.PixelsPerMeter;
        x = 1;
        y = 1;
    }
}

@Override
public void render(float delta) {
    super.render(delta);
    camMap.translate(x,y,0);
    camMap.update();

    // Render Map
    map.render(camMap);

    // Render RayHandler
    map.renderRayHandler(camMap);

    // Render fore layers
    map.renderForeLayers(camMap);
}

当我不调整大小时,结果是完美的。但是,当调整屏幕大小时,它变得很奇怪: Image

任何想法如何正确实施?

0 个答案:

没有答案