水平和垂直居中调整Actor会导致边界错误

时间:2018-05-30 20:29:36

标签: animation libgdx scale centering

我的舞台中央有一个按钮,想要在用户开始敲击它时立即从中心缩放,然后在释放手指后立即重置音阶。

这是我使用的代码:

private int size;
private Texture texture;
private Action scaleAction, scaleDownAction;

public PlayButtonActor(int x, int y, int size) {
    this.size = size;
    setBounds(x, y, size, size);
    this.texture = new Texture("data/play_button.png");
    this.setTouchable(Touchable.enabled);
    setOrigin(size / 2, size / 2);

    final float finalSize = size;
    addListener(new InputListener() {
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            event.handle();
            removeAction(scaleAction);
            scaleDownAction = Actions.parallel(
                    Actions.scaleTo(1f, 1f, 0.1f)
            );
            addAction(scaleDownAction);
        }

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            removeAction(scaleDownAction);
            scaleAction = Actions.parallel(
                    Actions.scaleTo(1.4f, 1.4f, 0.2f)
            );
            addAction(scaleAction);
            return true;
        }
    });
}

@Override
public void draw(Batch batch, float parentAlpha) {
    final float realSize = this.getScaleX() * size;
    batch.draw(this.texture, this.getX() - realSize / 2, this.getY() - realSize / 2, realSize, realSize);
}

这可以成功缩放按钮,但只能点击按钮的右上象限。Here is a graphic showing it.

如何制作按钮以使按钮从中心缩放到中心,同时它的边界始终是正确的?

提前致谢!

1 个答案:

答案 0 :(得分:0)

不要设置纹理,但要将纹理添加为图像:

添加(新图像(纹理)); 然后缩放图像。可点击区域将始终是完整的按钮大小,与图像缩放无关。