Libgdx单阶段的背景和前景

Libgdx单阶段的背景和前景

本文介绍了Libgdx单阶段的背景和前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求:

  1. 填充整个物理屏幕的背景(必要时进行拉伸)
  2. 保留前景资产的长宽比(使用虚拟宽度和高度)

为此,我在屏幕上使用了两个阶段,如下面的代码所示.

For this, I use two Stages in my Screen as shown in the code below.

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对象很重!)

In all the tutorials I have read, I have only seen one stage being used for each screen. So, how do I fulfill both the requirements in single stage? Is it too expensive to have separate stages? (I have read that SpriteBatch objects are heavy!)

这是我解决问题的方式:

要摆脱背景阶段,我更新了渲染和调整大小功能,如下所示.基本上,我将背景的右下角移至(-gutterWidth,-gutterHeight),并向纹理区域的区域宽度和高度添加了两倍于沟槽宽度和高度的值.舞台已经走了:-)

To get rid of background stage, I updated my render and resize functions as follows. Basically, I shifted the bottom right corner of background to (-gutterWidth,-gutterHeight) and added twice the values of gutter width and height to region width and height of the texture region. The stage is gone now :-)

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

推荐答案

您可以使用两个阶段,但对于您而言,最好在一个阶段中创建两个组来解决此问题:

You could use two stages, but for your case it would be better to just solve this problem by creating two groups inside a single stage:

Stage stage = new Stage();

Group background = new Group();
background.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Group foreground = new Group();
foreground.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

// Notice the order
stage.addActor(background);
stage.addActor(foreground);

foreground.addActor(new Actor());
// Or anything else you want to add like you normally would to the stage.

background.addActor(new Image()); // your background image here.

Gdx.input.setInputProcessor(stage);

这篇关于Libgdx单阶段的背景和前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:13