加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

屏幕 – Libgdx背景和单一阶段的前景

发布时间:2020-12-14 00:50:22 所属栏目:百科 来源:网络整理
导读:我的要求: 背景填充整个物理屏幕(如果需要,可以拉伸) 保留前景资产的纵横比(使用虚拟宽度和高度) 为此,我在屏幕中使用两个阶段,如下面的代码所示. public void render(float delta) { backgroundStage.act(delta); backgroundStage.draw(); foregroundStage
我的要求:

>背景填充整个物理屏幕(如果需要,可以拉伸)
>保留前景资产的纵横比(使用虚拟宽度和高度)

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

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.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,true);
    gw = foregroundStage.getGutterWidth();
    gh = foregroundStage.getGutterHeight();
    foregroundStage.getCamera().translate(-gw,0);
    }
您可以使用两个阶段,但对于您的情况,通过在单个阶段内创建两个组来解决此问题会更好:
Stage stage = new Stage();

Group background = new Group();
background.setBounds(0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
Group foreground = new Group();
foreground.setBounds(0,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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读