Niranjana
Niranjana

Reputation: 536

Using Actions on a Button-LibGdx

I have a play button like this;

    private void drawPlayButton() {
        playButton = new ImageButton(new TextureRegionDrawable(playTexture), new TextureRegionDrawable(pressTexture));
        stage.addActor(playButton);

        playButton.setPosition(UIConstants.PLAY_X, UIConstants.PLAY_Y, Align.center);

        playButton.addListener(new ActorGestureListener() {
            @Override
            public void tap(InputEvent event, float x, float y, int count, int button) {
                super.tap(event, x, y, count, button);

                game.setScreen(new GameScreen(game));

            }
        });
    }

I want to add scale in and out effect to this button using Actions.

I am not that familiar with Actions,I tried something like this;

float duationsec= 0.5f;
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),    
Actions.scaleTo(1f, 1f, duationsec)));
img.setOrigin(Align.center);
stage.addActor(playButton);

But this is not giving any effect to the button also same code works for an Image.Is this because button uses drawable texture?

How can I give this effect to a button using Actions?

Also my code is working for one time only.I am calling it in show().If I call it in render(),it works in an abnormal manner.I want this effect to be displaying forever.

Is it possible to achieve this? Any help would be appreciated.

Upvotes: 2

Views: 898

Answers (1)

AAryan
AAryan

Reputation: 20140

For performance reason most scene2d.ui groups have transform set to false by default and ImageButton is part of that ui group so you've to enable transform by using setTransform(..) method.

For more detail you can check
https://libgdx.com/wiki/graphics/2d/scene2d/scene2d-ui#rotation-and-scale

float duationsec= 0.5f;

playButton.setOrigin(Align.center);
playButton.setTransform(true);             //  <--  Enable Transform
stage.addActor(playButton);
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec)));  

EDIT

If you want to scale up/down on user Click, Do in this way :

playButton.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {

            playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec), Actions.run(new Runnable() {
                @Override
                public void run() {
                    game.setScreen(new GameScreen(game));
                }
            })));

            super.clicked(event, x, y);
        }
    });

Upvotes: 3

Related Questions