LibGDX 中的 FitViewport 背景图片

4

我正在使用LibGDX开发我的应用程序。

我使用FitViewport来确保16:9的宽高比。因此,与16:9不同宽高比的玩家将在两侧留有黑色边框。

最好的方法是什么,可以绘制一张填充整个屏幕的背景图片,同时覆盖黑色边框区域?

    camera = new OrthographicCamera();
    viewport = new FitViewport(WIDTH, HEIGHT, camera);
    viewport.apply();
    camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
    camera.update();

这是我目前设置相机/视口的方法。

然后我使用SpriteBatch在其上绘制东西。

Gdx.gl.glClearColor(1, 1, 1, 1);

目前我至少可以将黑色条的颜色更改为任何RGB颜色。

1个回答

3

在我看来,最好的想法是创建第二个阶段和它自己的Viewport,仅用于背景目的。这个第二个Viewport不应该是FillViewport - 根据我的经验,它会拉伸你的图形。我认为在这种情况下使用ExtendViewport更好。

那么它应该是什么样子:

    Stage stage, backStage;
    FitViewport viewport;
    ExtendViewport backViewport;

    ...

    stage = new Stage(); //this is your normal stage you have now
    stage.setViewport( yourFitViewport ); //here you are assingning fit viewport

    backViewport = new ExtendViewport( screenWidth, screenHeight );

    backStage = new Stage();
    backStage.setViewport( backViewport ); 

    ...
    //now add to backStage your background Image

    backStage.addActor( yourBackground );

现在只需在渲染方法中处理新的阶段即可。
    backStage.act();
    stage.act();

    backStage.draw(); //backStage first - we want it under stage
    stage.draw();

只需像旧的一样在更新或渲染中更新新的Viewport。就这些。

在此处了解更多有关Viewports的信息:https://github.com/libgdx/libgdx/wiki/Viewports


舞台上不能将纹理或精灵添加为演员。 不过,我仍然可以通过ExtendViewport实现,谢谢。 - Tavados
是的,但是 new Image(Texture texture); 是。 - m.antkowicz
@m.antkowicz 你有什么想法为什么“这个第二个视口不应该是FillViewport”? - best wishes
@祝一切顺利 - 因为填充视口会拉伸图形,而不同的显示器具有不同的比例,结果可能会是一场悲剧 :) - m.antkowicz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接