Android Studio Libgdx精灵/纹理透明度



我需要改变纹理/精灵的透明度,我真的不知道该怎么做。任何帮助吗?

我看到了两个解决方案——一个是直接在精灵中改变透明度:

    Sprite sprite = new Sprite( yourTexture );              
    Sprite otherSprite = new Sprite( yourOtherTexture );
    sprite.setAlpha(0.1f); // 0f is full transparent when 1f is full visible
    ...
    batch.begin();
    sprite.draw( batch ); //this one will be almost transparent
    otherSprite.draw( batch ); //this one will be normal
    batch.end();

你可以直接操作Sprite color:

    sprite.setColor( sprite.getColor.r, sprite.getColor.g, sprite.getColor.b, 0.1f);

但这对我来说似乎是一种愚蠢的方式。

我推荐的第二个方法是将Sprite打包到Scene2d Actor类中,例如Image。然后你仍然可以直接改变aplha,但你也可以使用Scene2d Actions机制,平滑地操纵演员的alpha(就像alpha一步一步地改变动画)。

Scene2dActions都太宽了,不能在这里描述它,但我鼓励你在这里阅读:

  • Scene2d
  • <
  • Scene2d行动/gh>

我把这个添加到我的gamerender .java:

private void drawTapTapChuggy() {
    batcher.setColor(1, 1, 1, 0.5f);
    batcher.draw(AssetLoader.tapTapChuggy, 28, 89, (83 * 0.9f), (52 * 0.9f));
    batcher.setColor(1, 1, 1, 1.0f);
}

最新更新