Reputation: 119
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)));
}});
I want to scroll my table up and down by dragging my table (like Facebook on phone) I can't make this math thing on the Y
I don't know why when I drag it it goes crazy.
Upvotes: 2
Views: 137
Reputation: 1098
Parameters x
and y
in the methods of InputListener
are local coordinates (i.e. relative to your table
), so you need to convert them to table
's parent coordinate system:
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);
}
});
By the way, using ScrollPane
will probably be much better and convenient solution in your case: https://github.com/libgdx/libgdx/wiki/Scene2d.ui#scrollpane
Upd:
Actually, since you only need deltaY
you can do this without converting coordinates:
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);
}
});
Upvotes: 2