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
上做这个数学事情我不知道为什么当我拖动它时它变得疯狂
答案 0 :(得分:2)
x
方法中的参数y
和InputListener
是本地坐标(即相对于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);
}
});