2017-05-27 56 views
0

我試圖讓鼠標點擊+拖動結果移動正交相機相對於初始點擊。用鼠標拖動移動正射相機

我下面的內容幾乎是我想要的。

但是,您可以看到,當我單擊藍色聖馬刁橋南部的區域時,相機會跳到一個奇怪的位置,然後允許相機在整個拖曳中平穩移動。

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; 
} 

縮放看起來不會影響拖動動作的正確性,因爲它被考慮在內。然而,似乎最初的點擊並沒有像預期的那樣起作用,而是因爲它最初的跳躍並跳入一個看似隨機的區域。

這可能是什麼原因造成的?

回答

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實際上被觸發,需要一些時間/移動。