Libgdx - 如何让对话淡出而不是整个舞台?

时间:2022-03-23 23:11:31

When I show a Dialog with

当我显示一个Dialog时

someDialog.show(someStage);

The dialog show with default FadeIn action. And the stage fade in too. So it make my background fade along with the stage.

该对话框显示默认的FadeIn操作。舞台也渐渐淡出。因此它使我的背景随着舞台逐渐消失。

How can I prevent this to happen?

我怎样才能防止这种情况发生?

Edit: Here my example code:

编辑:这里是我的示例代码:

In Screen show method:

在屏幕显示方法中:

Texture bg = new Texture(Gdx.files.internal(src));
stage = new Stage(game.getViewport(), batch);
stage.addActor(someDialog) //just normal dialog

render code:

@Override
    public void render(float delta) {
        super.render(delta);
        getBatch().begin();
        getBatch().draw(backgroundTexture, 0, 0, width, height);
        getBatch().end();
        getStage().act(delta);
        getStage().draw();
        time += delta;
    }

I show the dialog when some event trigger:

我在某个事件触发时显示对话框:

   dialog.show(getStage());

I have tried to put null as second parameter to the show method

我试图将null作为第二个参数放到show方法中

dialog.show(getStage(), null);

and make both stage and dialog not fade in but a want to keep the effect for only dialog or at least not the background.

并且使得舞台和对话框都不会淡入但是想要仅保留对话框的效果,或者至少不保留背景效果。

1 个解决方案

#1


Stage doesn't clean up the SpriteBatch's color when it's done, so when the loop comes back around, the last-used color is still applied to the sprite batch. In your case, the last used color happens to be the dialog's color.

舞台不会在完成后清理SpriteBatch的颜色,因此当循环返回时,最后使用的颜色仍然应用于精灵批处理。在您的情况下,最后使用的颜色恰好是对话框的颜色。

To fix this, add getBatch().setColor(Color.WHITE); before your getBatch().draw(backgroundTexture, 0, 0, width, height); line.

要解决此问题,请添加getBatch()。setColor(Color.WHITE);在你的getBatch()。draw之前(backgroundTexture,0,0,width,height);线。

And I don't think your whole stage is fading. Right now, your background texture has nothing to do with the stage, except that they're both sharing use of the SpriteBatch.

我不认为你的整个舞台正在消退。现在,你的背景纹理与舞台无关,只是它们都共享使用SpriteBatch。

#1


Stage doesn't clean up the SpriteBatch's color when it's done, so when the loop comes back around, the last-used color is still applied to the sprite batch. In your case, the last used color happens to be the dialog's color.

舞台不会在完成后清理SpriteBatch的颜色,因此当循环返回时,最后使用的颜色仍然应用于精灵批处理。在您的情况下,最后使用的颜色恰好是对话框的颜色。

To fix this, add getBatch().setColor(Color.WHITE); before your getBatch().draw(backgroundTexture, 0, 0, width, height); line.

要解决此问题,请添加getBatch()。setColor(Color.WHITE);在你的getBatch()。draw之前(backgroundTexture,0,0,width,height);线。

And I don't think your whole stage is fading. Right now, your background texture has nothing to do with the stage, except that they're both sharing use of the SpriteBatch.

我不认为你的整个舞台正在消退。现在,你的背景纹理与舞台无关,只是它们都共享使用SpriteBatch。