Java / libGDX - Actions.fadeIn()和Actions.fadeOut()的问题

时间:2016-08-30 21:43:57

标签: java libgdx scene2d

这是我第一次发帖,而且我是自学成才所以请温柔!

我使用游戏和屏幕课程在libGDX中制作炸弹人复制品游戏:

public class Main extends Game {
...

@Override
public void create() {
    levelScreen = new LevelScreen(playerCount, new int[playerCount]);
    levelScreen.level.addAction(Actions.sequence(Actions.alpha(0), Actions.fadeIn(2f)));
    this.setScreen(levelScreen);
}

然而,当游戏启动时,没有褪色效果。

public class LevelScreen implements Screen {
...

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0.1f, 0.5f, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    level.act();
    level.draw();
    batch.end();
}

我希望这个关卡可以从黑色中淡出,但它不是没有!

当圆形结束时,我想将此级别的屏幕淡出为黑色,然后从黑色淡入奖杯屏幕:

(来自主类)

@Override
public void render() {

    super.render();

        if (endRoundTimer <= 0) {
            trophyScreen = new TrophyScreen(playerCount, levelScreen.getScore());
            levelScreen.level.addAction(Actions.sequence(Actions.fadeOut(1), Actions.run(new Runnable() {
                @Override
                public void run() {
                    setScreen(trophyScreen);
                }
            })));
    }
}

我已尝试在TrophyScreen中使用show()方法:

public class TrophyScreen implements Screen {
...
@Override
public void show()  {
    stage.addAction(Actions.sequence(Actions.alpha(0), Actions.fadeIn(1)));
}

我已经做了大量的搜索并尝试了各种各样的事情,但没有快乐。我确定我在draw()或render()方法中某处遗漏了一些阻止淡入淡出动作发生的事情。

UPDATE1

@Override public void draw() { 
super.draw();
if (roundOver) {
this.getBatch().begin(); String s = String.format("%s", message);
font_text.draw(this.getBatch(), s, (90 + (2 * 30)), (this.getHeight() / 2));
this.getBatch().end();
}

3 个答案:

答案 0 :(得分:2)

For fading to work on actors, they must properly apply their own color's alpha in the draw method. And for an entire hierarchy of objects to fade at once, they must all also apply the parentAlpha parameter from the draw method signature.

So your draw method in any custom Actor subclass should look like this:

public void draw (Batch batch, float parentAlpha) {
    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    //..do drawing
}

If you are using a Sprite in your Actor instead of a TextureRegion (which I don't recommend due to redundancies) you must apply the color to the Sprite instead of Batch.

Note that this method of fading the whole game is not a "clean" fade. Any actors that are overlapping other actors will show through each other when the parent alpha is less than 1 during the fade. An alternative that would provide a clean-looking fade would be to draw a copy of your background (or black) over your entire scene and fade that instead.

答案 1 :(得分:1)

我假设Stage是扩展font_text的类的对象,并且你在舞台内创建一个控件,这很奇怪。您没有为BitmapFont应用颜色,我认为它是@Override public void draw() { super.draw(); if (roundOver) { getBatch().begin(); String s = String.format("%s", message); font_text.setColor(getRoot().getColor()) font_text.draw(this.getBatch(), s, (90 + (2 * 30)), (this.getHeight() / 2)); getBatch().end(); } }

解决方案,奇怪的方式

如果你想以这种方式去做,你需要这样的东西:

getRoot()

Group从Stage获取Group root,我们这样做,因为应用于Stage的每个操作实际上都应用于此Label元素。我们得到颜色(有alpha通道),我们将颜色复制到bitmapFont。

此解决方案很奇怪,因为您实际上是在Stage内创建Label。这是毫无意义的,演员在舞台上演,不在里面。

解决方案,好方法

你想画文字,对吗?所以只需使用stage = new Stage(); Label.LabelStyle labelStyle = new Label.LabelStyle(bitmapFont, Color.WHITE); Label label = new Label("Hi, I am a label!", labelStyle); stage.addActor(label); 作为演员,显示文本。演员为你做的工作:

stage.addAction(Actions.sequence(Actions.alpha(0), Actions.fadeIn(5)));
label.addAction(Actions.moveBy(0, 300, 15));

然后你可以应用动作并且它们将正常工作(并且每个演员都可以应用自己的动作)。

listModel.addAll(remainItemsList);
combobox.setModel(listModel);

有许多不同的演员,如TextButton,Image,ScrollPane。它们是可定制的,易于管理,可以集成在组和表中。

输出:

Animated output

答案 2 :(得分:0)

一种更好的方法是,首先在所有内容上绘制一个黑色图像,这样就不必弄乱每个场景对象的alpha。使用分层来做到这一点。 This post可能会有所帮助。

然后,您可以控制它的Alpha通道,在取消暂停游戏动作以使其返回绘制周期之前,将其渲染更改为0。在舞台结束时重新激活它,以淡出效果。

谢谢你,这样会更好。