代码之家  ›  专栏  ›  技术社区  ›  Niranjana

在按钮LibGdx上使用操作

  •  1
  • Niranjana  · 技术社区  · 7 年前

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

    我想使用动作为这个按钮添加缩放效果。

    我对动作不太熟悉,我试过这样的动作;

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

    我如何使用动作使按钮产生这种效果?

    此外,我的代码只工作了一次。我在show()中调用它。如果我在render()中调用它,它会以异常方式工作。我希望这种效果永远展现出来。

    有可能做到这一点吗? 任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  2
  •   AAryan    7 年前

    出于性能原因,most scene2d.ui 组具有 transform 默认设置为false,然后 ImageButton 是该ui组的一部分,因此您必须使用 setTransform(..) 方法

    有关更多详细信息,请查看
    https://github.com/libgdx/libgdx/wiki/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)));  
    

    编辑

    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);
            }
        });
    
    推荐文章