0
\$\begingroup\$

There is a button TextButton, there is also an animation spinAnimation. Neither can I understand how to implement the "fading animation" (i.e. when the button is clicked, the animation started, and gradually, after a certain time, stopped). Code Example:

@Override
public void create(){

textureAtlas = new TextureAtlas("pack.atlas"); 
stage = new Stage(new ScreenViewport());
sb = new SpriteBatch();

 final TextButton button = new TextButton("Start",skin , "default");
        button.setWidth(220);
        button.setHeight(50);


  button.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                spinAnimation.setFrameDuration(0.5f);
                int i = 0;


            }
        });

    stage.addActor(button);



           Gdx.input.setInputProcessor(stage);
    }

@Override
   public void render() {

   elepsedTime += Gdx.graphics.getDeltaTime();
   Gdx.gl.glClearColor(0,0,0,1);       
   Gdx.gl.glClear(com.badlogic.gdx.graphics.GL20.GL_COLOR_BUFFER_BIT);

   sb.begin();
   stage.act(Gdx.graphics.getDeltaTime());
   sb.draw(spinAnimation.getKeyFrame(elepsedTime,true), 50,  60


   sb.end();
   stage.draw();
    }
\$\endgroup\$
0
\$\begingroup\$

Keep a duration variable for as long as you want the animation to keep going. Also have another variable that checks if your animation has started. in your click listener switch started = true, and in your render loop do something like :

if(started){
    elapsed+=delta;
    if(elapsed < duration){
     //draw your animation
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thx. I found another method. Timer.schedule(new Timer.Task() { @Override public void run() { spinAnimation.setFrameDuration(0f); } },1); \$\endgroup\$ – Nexx Jan 6 '18 at 23:17
  • \$\begingroup\$ I think the reason you would not do it that way is because Timer runs on a separate thread but as long as everything works :) \$\endgroup\$ – Jonathan Camarena Jan 7 '18 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.