[Solved a Little] Image Ignoring Alpha Changes from Actions

Anything libgdx related goes here!

[Solved a Little] Image Ignoring Alpha Changes from Actions

Postby shatterblast » Fri Mar 15, 2019 3:44 am

In the following code, I am not able to use Actions.sequence() to change the alpha value of the Image. I have done some Google searching, but I am left confused. Changing the alpha value of the Stage does not seem to work. I assume it has something to do with the "parentAlpha" of the draw() method, because changing that value correctly works? However, changing the alpha value of the Image with setColor() refuses to work. I am not sure what to adjust.

Code: Select all
//Variables declared in the beginning of the class.
private float timer_ProgressBar_Background_01 = 0;
private float primaryDelta = 0;
private int FRAME_COUNT_OF_PROGRESSBAR_BACKGROUND_ANIMATION_01;

private TextureRegion frame_ProgressBar_Background_01;
private Animation<TextureRegion> progressBar_Background_Animation_01;
Image image_ProgressBar_Background_01;

private SpriteBatch buttonBackground_SpriteBatch;
private Stage buttonBackground_Stage;

// ... Other code ...

render() {

      // ... Other code ...

      //todo  This may have something to do with the .act() method of the Image object.
      //**************************************
      timer_ProgressBar_Background_01 += getPrimaryDelta();
      frame_ProgressBar_Background_01 = progressBar_Background_Animation_01.getKeyFrame(timer_ProgressBar_Background_01, true);

      if (timer_ProgressBar_Background_01 > FRAME_COUNT_OF_PROGRESSBAR_BACKGROUND_ANIMATION_01 ) {

          timer_ProgressBar_Background_01 = 0;

      }
      //**************************************


      //todo  Test SpriteBatch.
      buttonBackground_SpriteBatch.setProjectionMatrix(orthoCamera.combined);
      buttonBackground_SpriteBatch.begin();
      buttonBackground_SpriteBatch.enableBlending();


      //"frame_ProgressBar_Background_01" is a TextureRegion.
      image_ProgressBar_Background_01 = new Image( frame_ProgressBar_Background_01 );
      image_ProgressBar_Background_01.setPosition( 569, 100 );
      image_ProgressBar_Background_01.draw( buttonBackground_SpriteBatch, 1f );  //The 1f sets the "parentAlpha" to full.

      //The below line causes an error when enabled.       
      //image_ProgressBar_Background_01.act( getPrimaryDelta() );


      //Commented out.      
      //Color imageColor_01 = image_ProgressBar_Background_01.getColor();
      //image_ProgressBar_Background_01.setColor(imageColor_01.r, imageColor_01.g, imageColor_01.b, imageColor_01.a * 0);


      //THIS DOES NOT CHANGE THE ALPHA VALUE.      
      image_ProgressBar_Background_01.addAction(

            Actions.sequence(

                  //Actions.fadeIn( separation.getABILITIES_FADE_IN_AND_FADE_OUT_TIME_LENGTH() )
                  Actions.alpha( 0f )

            )

      );


      buttonBackground_SpriteBatch.end();

      buttonBackground_Stage.act();
      buttonBackground_Stage.draw();

      // ... Other code

}


Thank you for your insight.

Edit: I updated the example code above.
Last edited by shatterblast on Sat Mar 16, 2019 5:22 pm, edited 6 times in total.
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm

Re: Image Ignoring Alpha Changes from Actions

Postby evilentity » Fri Mar 15, 2019 10:24 am

Is the blending on? Set a breakpoint in image and verify that it changes. If it doesnt you dont act() the actor/stage.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4687
Joined: Wed Aug 24, 2011 11:37 am

Re: Image Ignoring Alpha Changes from Actions

Postby shatterblast » Fri Mar 15, 2019 3:17 pm

evilentity wrote:Is the blending on?

Yes.

evilentity wrote:Set a breakpoint in image and verify that it changes.

I am not sure how to do that, but I will learn.

evilentity wrote:If it doesnt you dont act() the actor/stage.

The Stage uses it, but I did not with the Image. When I try to use act() with Image, it requires a float. Adding a delta does not seem to affect the already working animation, though. However, if I do not use act() at all with either the Stage or Image, the animation still works regardless. I update the TextureRegion separately, so maybe that is the issue? My updated code in the original post above shows that.


I have experimented with the following changes:

Code: Select all
timer_ProgressBar_Background_01 += getPrimaryDelta();


//USING JUST ONE OF ANY OF THE BELOW LINES DOES NOT SEEM TO CHANGE ANYTHING.
image_ProgressBar_Background_01.act( timer_ProgressBar_Background_01 );
image_ProgressBar_Background_01.act( getPrimaryDelta() );
image_ProgressBar_Background_01.act( 0 );

buttonBackground_Stage.act( timer_ProgressBar_Background_01 );
buttonBackground_Stage.act( getPrimaryDelta() );
buttonBackground_Stage.act( 0 );


I updated the code in the original post.
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm

Re: Image Ignoring Alpha Changes from Actions

Postby evilentity » Fri Mar 15, 2019 3:51 pm

For actions to work stage/actor must be updated by calling act(delta). Since your animations are working, presumably you are doing that already.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4687
Joined: Wed Aug 24, 2011 11:37 am

Re: Image Ignoring Alpha Changes from Actions

Postby shatterblast » Fri Mar 15, 2019 4:04 pm

evilentity wrote:For actions to work stage/actor must be updated by calling act(delta). Since your animations are working, presumably you are doing that already.

It may have to do with how I setup the animations, since they and also the separate individual Spine animations work even if I do not use ANY act() at all, anywhere in my game. It DOES correctly affect all the Scene2D stuff in a decent way, though. I added the Image stuff later as an "after thought" for the purpose of using Action. I originally believed that using act() with the right Stage would be fine, but somehow... since the animations are outside the influence of act(), I guess Action will not affect them?

I am not sure yet how to re-write the code so that act() controls the animations or, at least, influences them. I could maybe extend Image to affect the "parentAlpha" that its draw() method references. I have only tried the alpha stuff of Action, though, so I do not know how its other items might behave.

Edit: Something I am doing is not right, because none of the Actions work at all on the Image.
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm

Re: Image Ignoring Alpha Changes from Actions

Postby evilentity » Fri Mar 15, 2019 7:57 pm

Do you add the image to stage or other container that is contained in one? If not, act wont call itself.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4687
Joined: Wed Aug 24, 2011 11:37 am

Re: Image Ignoring Alpha Changes from Actions

Postby shatterblast » Fri Mar 15, 2019 8:49 pm

evilentity wrote:Do you add the image to stage or other container that is contained in one? If not, act wont call itself.

Bleh! I seem to successfully render a TextureRegion through an Animation<TextureRegion> with my SpriteBatch on accident. The TextureRegion also separately at the same time goes out through an Image, which does not handle Animation. The rendering works, even with the separate object, because the SpriteBatch still reaches the TextureRegion through Image, even if I do not seem to have a SpriteBatch directly attached to the Animation<TextureRegion>. This explains why I can use setPosition() through an Image, but Actions do not work. The Animation<TextureRegion> does the work, not Image. Now, I am not sure what to do.
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm

Re: Image Ignoring Alpha Changes from Actions

Postby evilentity » Fri Mar 15, 2019 9:05 pm

I guess if you mix scene2d and non scene2d stuff you will have a bad time?
Do this image_ProgressBar_Background_01.act( getPrimaryDelta() ); if you really must... in each update(). not once.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4687
Joined: Wed Aug 24, 2011 11:37 am

Re: Image Ignoring Alpha Changes from Actions

Postby shatterblast » Fri Mar 15, 2019 9:29 pm

evilentity wrote:I guess if you mix scene2d and non scene2d stuff you will have a bad time?

Right, I did that on accident.

evilentity wrote:Do this image_ProgressBar_Background_01.act( getPrimaryDelta() ); if you really must... in each update(). not once.

If I understand, I have tried that, and it is not working. At least with the "parentAlpha" variable in the draw() method of Image, it shows I can still touch the alpha while the Animation does its thing.
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm

Re: Image Ignoring Alpha Changes from Actions

Postby shatterblast » Sat Mar 16, 2019 5:15 pm

I ended up putting the following code to use for loading an image from 0% alpha to full alpha in the very beginning of a battle:

Code: Select all
// ... Variables in the beginning of the class ...
float alphaValue_Image_ProgressBar_Background_01 = 0;
Boolean oldStyle_FadeIn_Repeat = true;

// ... Other code ...

   void oldStyle_FadeIn_for_Image_ProgressBar_Background_Group( float seconds ) {

      if( oldStyle_FadeIn_Repeat ) {

         if ( this.alphaValue_Image_ProgressBar_Background_01 < 1 ) {

            this.alphaValue_Image_ProgressBar_Background_01 = alphaValue_Image_ProgressBar_Background_01 +
                  ( 0.1f * getPrimaryDelta() * seconds);
            
            System.out.println("alphaValue = " + this.alphaValue_Image_ProgressBar_Background_01);

            Color color = image_ProgressBar_Background_01.getColor();
            image_ProgressBar_Background_01.setColor( color.r, color.g, color.b, this.alphaValue_Image_ProgressBar_Background_01 );

         } else {

            this.alphaValue_Image_ProgressBar_Background_01 = 1f;
            System.out.println("alphaValue = " + this.alphaValue_Image_ProgressBar_Background_01);

            Color color = image_ProgressBar_Background_01.getColor();
            image_ProgressBar_Background_01.setColor( color.r, color.g, color.b, this.alphaValue_Image_ProgressBar_Background_01 );

            oldStyle_FadeIn_Repeat = false;

         }

      }

   }


I like long variable names a bit too much, and my math is a bit off for the above. :roll:

Anyhow, thank you for your efforts. @evilentity :)
shatterblast
 
Posts: 200
Joined: Sun Jul 06, 2014 1:14 pm


Return to Libgdx

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 1 guest