我的要求:
- 背景填充整个物理屏幕(必要时进行拉伸)
- 保持前景资产的纵横比(使用虚拟宽度和高度)
为此,我在屏幕中使用了两个阶段,如下面的代码所示。
public void render(float delta) {
backgroundStage.act(delta);
backgroundStage.draw();
foregroundStage.act(delta);
foregroundStage.draw();
}
public void resize(int width, int height) {
background.setWidth(width);
background.setHeight(height);
backgroundStage.setViewport(width, height, true);
foregroundStage.setViewport(MainGame.WIDTH, MainGame.HEIGHT, true);
foregroundStage.getCamera().position.set(-foregroundStage.getGutterWidth(),-foregroundStage.getGutterHeight(),0);
}
在我所阅读的所有教程中,我只看到每个屏幕只使用了一个舞台。那么,如何在单个舞台中满足这两个要求呢?拥有单独的舞台会太昂贵吗?(我已经了解到SpriteBatch对象很重!)
这是我解决问题的方法:
为了摆脱背景舞台,我更新了我的渲染和调整窗口大小的函数。基本上,我将背景的右下角移动到了(-gutterWidth,-gutterHeight),并将纹理区域的宽度和高度值加倍,再加上两个边距值。现在舞台被排除了 :-)
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 0, 0.5f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
foregroundStage.getSpriteBatch().begin();
foregroundStage.getSpriteBatch().draw(backgroundImage,-gw,-gh,backgroundImage.getRegionWidth()+2*gw,backgroundImage.getRegionHeight()+2*gh);
foregroundStage.getSpriteBatch().end();
foregroundStage.act(delta);
foregroundStage.draw();
public void resize(int width, int height) {
screenW = width;
screenH = height;
foregroundStage.setViewport(MainGame.WIDTH, MainGame.HEIGHT, true);
gw = foregroundStage.getGutterWidth();
gh = foregroundStage.getGutterHeight();
foregroundStage.getCamera().translate(-gw,-gh,0);
}