FitViewport 中的 LibGDX 背景图像

2023-03-01Java开发问题
4

本文介绍了FitViewport 中的 LibGDX 背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以,我正在为即将推出的应用程序使用 LibGDX.

So, I'm using LibGDX for my upcoming App.

我使用 FitViewport 来确保 16:9 的纵横比.因此,除 16:9 以外的其他宽高比的播放器将在站点出现黑条.

I use a FitViewport to ensure the 16:9 aspect ratio. So players with other aspect ratios than 16:9 will have black bars at sites.

最好的方法是绘制一个屏幕填充背景图像,它也覆盖了黑条所在的区域?

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

这就是我目前设置相机/视口的方式.

Thats how I set up my camera/viewport currently.

然后我用 SpriteBatch 在上面画东西.

I then draw stuff on it with a SpriteBatch.

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

这就是我目前至少将黑条的颜色更改为任何 RGB 颜色的方式.

That's how I currently at least change the color of the black bars, to any RGB color.

推荐答案

在我看来,最好的办法是创建第二个阶段,它自己的 Viewport 仅用于后台用途.第二个 Viewport 不应该是 FillViewport - 它会根据我的经验拉伸您的图形.我认为 ExtendViewport 在这种情况下会更好.

In my opinion, the best idea is to create a second stage and it's own Viewport only for background purposes. This second Viewport should not be FillViewport - it will strech your graphics from my experience. I think ExtendViewport is better in this case.

那么它应该是什么样子:

So how should it look:

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

现在只需在渲染方法中处理新阶段.

Now just handle new stage in the render method.

    backStage.act();
    stage.act();

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

并在更新中更新新的 Viewport 或像旧的一样渲染.就是这样.

And update new Viewport in update or render like your old one. That's all.

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

Read more about Viewports here: https://github.com/libgdx/libgdx/wiki/Viewports

这篇关于FitViewport 中的 LibGDX 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13