Java Libgdx GestureDetector pan方法在设备中速度较慢/较快
我在libgdx中有一个平铺贴图,我正在类中实现GestureDetector pan方法 我想发生的是,当用户使用平移方法时,屏幕被拖动到该方向,有点像用户用手指移动平铺地图 当我在我的桌面上测试代码时,拖动是非常好的,地图以完美的速度和平滑度拖动 但是,当我在不同的android设备上测试相同的代码时,拖动的速度要么太慢,要么太快。有时,小手指的移动会在整个屏幕上移动地图 如何使pan方法在所有设备上保持一致?我的代码:Java Libgdx GestureDetector pan方法在设备中速度较慢/较快,java,android,libgdx,drag,pan,Java,Android,Libgdx,Drag,Pan,我在libgdx中有一个平铺贴图,我正在类中实现GestureDetector pan方法 我想发生的是,当用户使用平移方法时,屏幕被拖动到该方向,有点像用户用手指移动平铺地图 当我在我的桌面上测试代码时,拖动是非常好的,地图以完美的速度和平滑度拖动 但是,当我在不同的android设备上测试相同的代码时,拖动的速度要么太慢,要么太快。有时,小手指的移动会在整个屏幕上移动地图 如何使pan方法在所有设备上保持一致?我的代码: public class SinglePlayerGame exten
public class SinglePlayerGame extends GestureDetector.GestureAdapter implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer mapRenderer;
private OrthographicCamera camera;
private Viewport viewport;
private Main game;
public SinglePlayerGame(Main game){
this.game = game;
this.camera = new OrthographicCamera();
this.viewport = new StretchViewport(Main.VIRTUAL_WIDTH, Main.VIRTUAL_HEIGHT, camera);
this.map = new TmxMapLoader().load("map_1.tmx");
this.mapRenderer = new OrthogonalTiledMapRenderer(map);
this.camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0);
Gdx.input.setInputProcessor(new GestureDetector(this));
resize(Main.VIRTUAL_WIDTH, Main.VIRTUAL_HEIGHT);
}
public boolean pan(float x, float y, float deltaX, float deltaY){
camera.translate(-deltaX, deltaY);
camera.update();
return false;
}
@Override
public void render(float delta){
update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.end();
mapRenderer.render();
}
public void update(){
camera.update();
mapRenderer.setView(camera);
}
@Override
public void resize(int width, int height){
viewport.update(width, height);
}
public void show(){}
public void pause(){}
public void resume(){}
public void hide(){}
@Override
public void dispose(){
}
GestureDetector单位以屏幕像素为单位,因此您需要根据相机和视口的定义将其缩放到世界单位:
public boolean pan(float x, float y, float deltaX, float deltaY){
float scaleX = viewport.getWorldWidth() / (float)viewport.getScreenWidth();
float scaleY = viewport.getWorldHeight() / (float)viewport.getScreenHeight();
camera.translate((int)(-deltaX * scaleX), (int)(deltaY * scaleY));
camera.update();
return false;
}