Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 316 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:

1  
2  
3  
4  
5  
      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:

1  
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:
1  
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.
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
1  
sprite.setAlpha(0.5f);
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:
1  
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:
1  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
//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
Offline Gibbo3771
« Reply #6 - Posted 2014-05-06 19:20:01 »

I do it like this

1  
2  
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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (76 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!