[Libgdx] Changing Alpha of a sprite doesn't show when drawn

I feel pretty stupid asking about this, since it seems very basic, but i’m stumped.

I have a sprite. I change its alpha in my update method:

		if (mSprite != null) {
			Color c = mSprite.getColor();
			c.a = 0;
			mSprite.setColor(c);
		}

As you can see I’m trying to make it transparent (just to see if it works). When I draw, I can still see the sprite.
Here’s my draw method:

batch.draw(mSprite.getTexture(), mSprite.getX()+(mXPixelOffset*mParallaxFactor), mSprite.getY(), mSprite.getOriginX(), 0, mSprite.getWidth(), mSprite.getHeight(), mScreenRatio, mScreenRatio, 0, 0, 0, mSprite.getTexture().getWidth(), mSprite.getTexture().getHeight(), false, false);

Here’s the draw method signature just in case:

void com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Update is happening before I do batch.begin()

What am I missing? Thanks in advance.

may be you forgot to set the spriteBatch.enableBlending()

It’s enabled by default, but I tried enabling it explicitly without any luck. Thanks for the suggestion though.

I thought that the alpha of a sprite i setting by

sprite.setAlpha(0.5f);

Not sure why, but I don’t have that method available to me.
However, I would think that both methods should accomplish it the same ???

The problem is that you don’t draw it correctly. You are just drawing a texture, so if you want to set the alpha to 0, you would have to add this line before your actual drawing code:


batch.setColor(c);

But like this you would not need the sprite class at all. A Texture or TextureRegion class would suffice.

It you want to take advantage of the sprite class, then you would draw it like this:


sprite.draw(batch);

Then the color of the sprite, but also the dimensions and positioning (maybe also rotation, not sure about that) would be taken into account. This means that you do not set the position and size in the draw method, but you set them as sprite properties:


//Position a sprite at 0/0 with size 64/64
sprite.setBounds(0, 0, 64, 64);

//Or

//Position a sprite at 0/0
sprite.setPosition(0, 0);
//Set size 64/64
sprite.setSize(64, 64);


Hope this helps

I do it like this



float alpha = 0;
sprite.setColor(sprite.getColor().r, sprite.getColor().g, sprite.getColor().b, alpha)


Seems to work, for my uses it would be to have the alpha of say an image fade to 0.20 and back to 1.0 to give it a “Glowing effect”, I use it mostly for fonts.

Thanks, atombrot. That was it. Now to deal with new issues!