按下后,如何淡出TextButton?

时间:2014-09-08 03:11:49

标签: libgdx

我只是想在我按下它之后淡出(不透明度=完全透明)一个按钮......以下代码具有无法解释的效果,即将背景图像(一个完全不同的演员!)淡化为黑色!

我做错了什么?

      Skin skin = new Skin();
      TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("gfx/menu_buttons.pack"));
      skin.addRegions(buttonAtlas);
      TextButtonStyle textButtonStyle = new TextButtonStyle();
      textButtonStyle.font = mButtonSpacefont;
      textButtonStyle.up = skin.getDrawable("menu_button_background_top");
      textButtonStyle.down = skin.getDrawable("menu_button_background_bottom");
      mButtonStartGame = new TextButton("Play", textButtonStyle);
      mButtonStartGame.setPosition(game.getIdealWidth()/2 - mButtonStartGame.getWidth()/2,
            game.getIdealHeight()/2 - mButtonStartGame.getHeight()/2);

      mButtonStartGame.addListener(new ChangeListener()
      {
         @Override
         public void changed (ChangeEvent event, Actor actor)
         {
            //triggers when button is pressed and let go
            mButtonStartGame.addAction(Actions.fadeOut( 1.0f ));
          }
      });

      // add actors to stage
      mStage.addActor(backgroundActor);//background
      mStage.addActor(titleActor);//ontop of background
      mStage.addActor(mButtonStartGame);//ontop of background

更新1: 我发现TextButton已经淡出 - 但是 - 包含背景图像backgroundActor的演员也是如此!当代码甚至不调用backgroundActor.addAction(Actions.fadeOut( 1.0f ));时,为什么backgroundActor会褪色?

更新2: render()方法似乎起了作用......当我调用Gdx.gl.glClearColor(0, 0, 0, 0);时,整个屏幕(除了titleActor!)由于fadeOut操作而变黑。当我呼叫Gdx.gl.glClearColor(1, 1, 1, 1);时,整个屏幕(除了titleActor!)变成白色。

@Override
public void render(float delta)
{
    Gdx.gl.glClearColor(0, 0, 0, 0);//clear the screen black
    //Gdx.gl.glEnable(GL20.GL_BLEND);// no effect, so commented out
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // update the actors
    mStage.act(Gdx.graphics.getDeltaTime());

    // draw the actors
    mStage.draw();
}

1 个答案:

答案 0 :(得分:0)

解决方案是我的所有演员都需要考虑parentAlpha并将颜色设置为batch.setColor()。 由于我的背景是一个演员,而我没有设置批量颜色,背景被搞砸了。

例如:

public class ActorBackground extends Actor
{
    // Member Variables
    private Texture mTexture;

    // Constructor
    public ActorBackground(GFXLoader gfxloader)
    {
        mTexture = new Texture(gfxloader.loadGraphic("menu_background"));// returns Gdx.files.internal(file.path())
        //setBounds(getX(), getY(), mTexture.getWidth(), mTexture.getHeight());
        setWidth(mTexture.getWidth());
        setHeight(mTexture.getHeight());
    }

    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        // needed to make actions work
        super.act(Gdx.graphics.getDeltaTime());

        // needed to make fade work
        Color color = new Color(this.getColor().r, this.getColor().g, this.getColor().b, this.getColor().a * parentAlpha);
        batch.setColor(color);

        // show it
        batch.draw(mTexture, getX(), getY(), getWidth(), getHeight());
    }
}