【问题标题】:LibGDX Fade Out / Fade In Screen TransitionLibGDX 淡出/淡入屏幕转换
【发布时间】:2017-07-21 08:50:30
【问题描述】:

我正在尝试在我的两个屏幕之间进行良好的淡入/淡出,经过数小时的在线搜索后,我空手而归。我尝试了各种涉及 Actions 的解决方案均无济于事,而且我并不热衷于使用 TweenEngine,但我将不胜感激!

以下是我找到的最接近的解决方案。这只是延迟屏幕切换之前的时间,但您看不到任何淡入淡出。

package com.aidanstrong.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.ColorAction;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;



public class FadingGame extends Game {
GameScreen gameScreen;
UpgradeScreen upgradeScreen;

private Actor fadeActor = new Actor();
private ShapeRenderer fadeRenderer;


@Override
public void create() {
    gameScreen = new GameScreen(this);
    upgradeScreen = new UpgradeScreen(this);
    setScreen(gameScreen);

    fadeRenderer = new ShapeRenderer(8);

}

public void setScreenWithFade (final Screen screen, float duration) {



    fadeActor.clearActions();
    fadeActor.setColor(Color.CLEAR);
    fadeActor.addAction(Actions.sequence(
            Actions.color(Color.BLACK, duration/2f, Interpolation.fade),
            Actions.run(new Runnable(){public void run(){setScreen(screen);}}),
            Actions.color(Color.CLEAR, duration/2f, Interpolation.fade)
    ));
}

@Override
public void render (){
    super.render();

    fadeActor.act(Gdx.graphics.getDeltaTime());
    float alpha = fadeActor.getColor().a;
    if (alpha != 0){
        fadeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        fadeRenderer.setColor(0, 0, 0, alpha);
        fadeRenderer.rect(-1, -1, 2, 2); //full screen rect w/ identity matrix
        fadeRenderer.end();
    }
}

}

【问题讨论】:

  • 如果你将屏幕绘制到帧缓冲区,你可以用它做任何你想做的事情。
  • 您可以查看Learning Libgdx Game Development 2nd Edition book Chapter 9!

标签: java android libgdx transition fade


【解决方案1】:

在您的舞台上将 topLayer 添加为 Image 并在该 Actor 上添加动作。试试这个测试用例:

public class GdxTest extends Game {

    FirstScreen firstScreen;
    SecondScreen secondScreen;

    @Override
    public void create() {
        firstScreen=new FirstScreen();
        secondScreen=new SecondScreen();

        setScreen(firstScreen);
    }

    public static Texture getTexture(){

        Pixmap pixmap;
        try {
            pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        }catch (GdxRuntimeException e)
        {
            pixmap=new Pixmap(1,1, Pixmap.Format.RGB565);
        }
        pixmap.setColor(Color.WHITE);
        pixmap.drawRectangle(0,0,1,1);

        return new Texture(pixmap);
    }
} 

第一屏

public class FirstScreen extends ScreenAdapter {

    Stage stage;
    Texture texture,white;

    @Override
    public void show() {

        stage=new Stage();

        final Image image = new Image(texture=new Texture("badlogic.jpg"));
        image.setSize(200, 200);
        image.setPosition(stage.getWidth()/2, stage.getHeight()/2, Align.center);
        stage.addActor(image);

        final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
        topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        topLayer.setColor(Color.BLACK);
        stage.addActor(topLayer);

        stage.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y) {

                topLayer.addAction(Actions.sequence(Actions.color(Color.BLACK,2),Actions.run(new Runnable() {
                    @Override
                    public void run() {

                        GdxTest gdxTest=((GdxTest)Gdx.app.getApplicationListener());
                        gdxTest.setScreen(gdxTest.secondScreen);
                    }
                })));

                super.clicked(event, x, y);
            }
        });

        topLayer.addAction(Actions.fadeOut(2));
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        stage.dispose();
        texture.dispose();
        white.dispose();
    }
}

第二屏

public class SecondScreen extends ScreenAdapter{

    Stage stage;
    Texture texture,white;

    @Override
    public void show() {

        stage=new Stage();

        final Image image = new Image(texture=new Texture("badlogic.jpg"));
        image.setSize(200, 200);
        image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
        image.setOrigin(Align.center);
        image.rotateBy(90);
        stage.addActor(image);

        final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
        topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        topLayer.setColor(Color.BLACK);
        stage.addActor(topLayer);

        topLayer.addAction(Actions.fadeOut(2));
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(0,0,0,0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();
        stage.act();
    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        stage.dispose();
        texture.dispose();
        white.dispose();
    }
}

或者

您可以在舞台顶部使用 FBO。

编辑

public class GdxTest extends ApplicationAdapter {

    Stage stage;
    FrameBuffer frameBuffer;
    float delta;

    @Override
    public void create() {

        stage=new Stage();
        {
            final Image image = new Image(new Texture("badlogic.jpg"));
            image.setSize(200, 200);
            image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
            stage.addActor(image);
        }

        {
            final Image image = new Image(new Texture("badlogic.jpg"));
            image.setSize(200, 200);
            image.setPosition(stage.getWidth()/3, stage.getHeight()/3, Align.center);
            stage.addActor(image);
        }

        {
            final Image image = new Image(new Texture("badlogic.jpg"));
            image.setSize(200, 200);
            image.setPosition(stage.getWidth()*.75f, stage.getHeight()*.75f, Align.center);
            stage.addActor(image);
        }

        Gdx.input.setInputProcessor(stage);
        delta=1;
    }

    @Override
    public void render() {

        if(delta>0)delta-=.01f;


        frameBuffer.begin();

        Gdx.gl.glClearColor(0,0,0,delta);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        frameBuffer.end();

        stage.act();
        stage.draw();

        stage.getBatch().setProjectionMatrix(stage.getBatch().getProjectionMatrix().idt());

        stage.getBatch().begin();
        stage.getBatch().draw(frameBuffer.getColorBufferTexture(),-1,1,2,-2);
        stage.getBatch().end();
    }

    @Override
    public void dispose() {
        stage.dispose();
        frameBuffer.dispose();
    }

    @Override
    public void resize(int width, int height) {
        if(frameBuffer !=null && (frameBuffer.getWidth()!=width || frameBuffer.getHeight()!=height )) {
            frameBuffer.dispose();
            frameBuffer=null;
        }

        if(frameBuffer==null){
            try {
                frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
            }catch (GdxRuntimeException e){
                frameBuffer=new FrameBuffer(Pixmap.Format.RGB565,width,height,false);
            }
        }
    }
}

【讨论】:

  • 我发现你的答案很难适用于我的情况,因为我想在切换时继续在屏幕之间来回淡出。无论如何你可以告诉我如何使用FBO吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
相关资源
最近更新 更多