FitViewport中的LibGDX背景图像 [英] LibGDX Background Image in FitViewport

查看:153
本文介绍了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.

推荐答案

在我看来,最好的想法是创建第二个阶段并且它自己的视口仅用于背景目的。第二个视口不应该是 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.

那么应该如何看待:

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

并在更新或渲染中更新新的视口喜欢你的旧的。这就是全部。

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

了解更多关于视口的信息: https://github.com/libgdx/libgdx/wiki/Viewports

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆