用鼠标拖动移动正交相机

时间:2017-05-27 00:52:18

标签: java libgdx

我试图通过鼠标点击+拖动结果相对于初始点击移动正交相机。

我在下面所拥有的几乎就是我所希望的。

然而,你可以看出,当我点击圣马特奥桥南面的蓝色区域时,相机会跳到一个奇怪的位置,然后让相机在整个拖动过程中平稳移动。

enter image description here

相机的所有当前操作都是通过带有InputProcessor接口的类完成的。

处理相机拖动的方法是#touchDown和#touchDragged。 #touchDragged考虑了#scrolled操作的地图缩放。

/**
 * Sets values at which mouse initially clicked to be used for moving the camera about
 *
 * @param screenX - The x coordinate, origin is in the upper left corner
 * @param screenY - The y coordinate, origin is in the upper left corner
 * @param pointer
 * @param button
 * @return
 */
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (button != 0) return false;
    pressedX = screenX;
    pressedY = screenY;
    return false;
}

/**
 * In the event of a drag, the camera position is altered to correspond to a drag relative to the initial click
 *
 * @param screenX - The x coordinate, origin is in the upper left corner
 * @param screenY - The y coordinate, origin is in the upper left corner
 * @param pointer
 * @return
 */
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    final float zoomMultiplier = camera.zoom / 20f;
    camera.position.set(map.getWidth() - screenX * zoomMultiplier, screenY * zoomMultiplier, 0);
    return false;
}

/**
 * Zooms into or out of the map based on which direction the user scrolls
 * @param amount - The intensity of the scroll
 * @return
 */
@Override
public boolean scrolled(int amount) {
    final float zoomAmount = amount * 1.15f;
    camera.zoom += zoomAmount;
    return false;
}

缩放似乎不会影响拖动动作的正确性,因为它被考虑在内。但是,似乎初始点击不能正常工作,因为它的初始跳跃和跳跃到一个看似随机的区域。

导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

您是否尝试过GestureDetector.GestureListener.pan

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    camera.translate(deltaX, -deltaY, 0);
    camera.update();
    return false;
}

您仍然需要使用缩放级别,但如果它是1:1,则应将拖动精确数量拖动。但是,在实际触发pan之前需要一些时间/动作。