Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Libgdx] Changing Alpha of a sprite doesn't show when drawn  (Read 1625 times)
0 Members and 1 Guest are viewing this topic.
Offline opilkin

Senior Newbie

« Posted 2014-05-06 15:45:19 »

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;

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 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.
Offline Frika_Dev

Junior Newbie

« Reply #1 - Posted 2014-05-06 15:49:27 »

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

Senior Newbie

« Reply #2 - Posted 2014-05-06 15:51:40 »

It's enabled by default, but I tried enabling it explicitly without any luck. Thanks for the suggestion though.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Frika_Dev

Junior Newbie

« Reply #3 - Posted 2014-05-06 15:53:51 »

I thought that the alpha of a sprite i setting by
Offline opilkin

Senior Newbie

« Reply #4 - Posted 2014-05-06 16:00:03 »

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  Huh
Offline atombrot
« Reply #5 - Posted 2014-05-06 17:37:32 »

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:

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:

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


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

Hope this helps
Offline Gibbo3771
« Reply #6 - Posted 2014-05-06 19:20:01 »

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline opilkin

Senior Newbie

« Reply #7 - Posted 2014-05-07 12:33:57 »

Thanks, atombrot. That was it. Now to deal with new issues!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Riven (29 views)
2015-07-27 16:38:00

Riven (16 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (10 views)
2015-07-27 12:23:39

BurntPizza (27 views)
2015-07-25 00:14:37

BurntPizza (39 views)
2015-07-24 22:06:39

BurntPizza (22 views)
2015-07-24 06:06:53

NoxInc (25 views)
2015-07-22 22:16:53

NoxInc (17 views)
2015-07-22 22:13:39

Jesse (39 views)
2015-07-22 03:10:36
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!