LibGDX Background Image in FitViewport(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 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FitViewport 中的 LibGDX 背景图像
				
        
 
            
        - 获取数字的最后一位 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 转换 ldap 日期 2022-01-01
 
						
						
						
						
						
				
				
				
				