Never Mind
Never Mind

Reputation: 297

Android Studio Libgdx Sprite/Texture Transparency

I need to change the transparency of a texture/sprite and I really have no idea how to do it. Any help?

Upvotes: 1

Views: 3553

Answers (2)

mmmartinnn
mmmartinnn

Reputation: 407

I added this to my GameRenderer.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);
}

Upvotes: 1

m.antkowicz
m.antkowicz

Reputation: 13571

I see two solutions here - one is to change transparency directly in sprite:

    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();

you can always manipulate with Sprite color directly:

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

but it seems to be a silly way for me.

The second one which I recommend is to wrap your Sprite into Scene2d Actor class like for example Image. Then you can still change aplha directly but you are also able to use Scene2d Actions mechanism, manipulating actor's alpha smoothly (like alpha changing animation step by step).

Both Scene2d and Actions are to wide to describe it here but I encourage you to read about it here:

Upvotes: 4

Related Questions