通过拖动表

时间:2018-01-26 05:21:20

标签: java scroll libgdx drag

 table1.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
           scroll=y;
            System.out.print(scroll);
            return true;
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {

            table1.setPosition((scroll+(y)));
        }});

我想通过拖动我的桌子来上下滚动我的桌子(就像手机上的Facebook一样)我无法在Y上做这个数学事情我不知道为什么当我拖动它时它变得疯狂

1 个答案:

答案 0 :(得分:2)

x方法中的参数yInputListener是本地坐标(即相对于table),因此您需要将它们转换为table父母坐标系:

table.addListener(new InputListener() {

    Vector2 vec = new Vector2();

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        table.localToParentCoordinates(vec.set(x, y));
        return true;
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
        float oldTouchY = vec.y;
        table.localToParentCoordinates(vec.set(x, y));
        table.moveBy(0f, vec.y - oldTouchY);
    }
});

顺便说一下,使用ScrollPane可能会更好,更方便地解决您的问题:https://github.com/libgdx/libgdx/wiki/Scene2d.ui#scrollpane

<强> UPD:

实际上,由于您只需要deltaY,因此无需转换坐标即可完成此操作:

table.addListener(new InputListener() {

    float touchY;

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        touchY = y;
        return true;
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
        // since you move your table along with pointer, your touchY will be the same in table's local coordinates
        float deltaY = y - touchY;
        table.moveBy(0f, deltaY);
    }
});