1
關於:我有一個面板顯示由多邊形和線構成的地圖。在這張地圖上,我應用了轉換,並提供了一種通過旋轉和移動地圖(第一人稱)來轉換地圖的方式。JavaFX應用轉換似乎阻止KeyEvents
應用於地圖的轉換似乎阻止(或至少放緩)KeyEvent處理了beeing。有時,即使我釋放了密鑰,應用程序仍然在旋轉。
另外我注意到旋轉變換對這個效果有更大的影響。
碰撞檢測對此沒有影響(因爲此時它被禁用)。
由於應用程序的結構,我將centerR綁定到pointerR。應用
centerR.bind(pointerR);
轉換:
mapElements = new Group();
Scale s = new Scale();
s.xProperty().bind(zoom);
s.yProperty().bind(zoom);
s.setPivotX(panelBounds.getWidth()/2);
s.setPivotY(panelBounds.getHeight()/2);
mapElements.getTransforms().add(s);
Translate t = new Translate();
t.xProperty().bind(new SimpleDoubleProperty(panelBounds.getWidth()/2).subtract(centerX));
t.yProperty().bind(new SimpleDoubleProperty(panelBounds.getHeight()/2).subtract(centerY));
mapElements.getTransforms().add(t);
Rotate r = new Rotate();
r.angleProperty().bind(centerR.multiply(-1));
r.pivotXProperty().bind(centerX);
r.pivotYProperty().bind(centerY);
mapElements.getTransforms().add(r);
的KeyListener-設置:
setOnKeyPressed(new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent event)
{
System.out.println(event);
if (event.getCode() == KeyCode.UP)
moveForwards = true;
if (event.getCode() == KeyCode.DOWN)
moveBackwards = true;
if (event.getCode() == KeyCode.LEFT)
rotateLeft = true;
if (event.getCode() == KeyCode.RIGHT)
rotateRight = true;
}
});
setOnKeyReleased(new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent event)
{
System.out.println(event);
if (event.getCode() == KeyCode.UP)
moveForwards = false;
if (event.getCode() == KeyCode.DOWN)
moveBackwards = false;
if (event.getCode() == KeyCode.LEFT)
rotateLeft = false;
if (event.getCode() == KeyCode.RIGHT)
rotateRight = false;
}
});
OnFrame-旋轉:
private final double fps = 20;
private final KeyFrame frame = new KeyFrame(Duration.millis(1000/fps), new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
double movement = ((moveForwards ? 3 : 0)
+ (moveBackwards ? -3 : 0))
* movementSpeed;
double rotation = ((rotateLeft ? -3 : 0)
+ (rotateRight ? 3 : 0))
* rotateSpeed;
double oldX = pointerX.get();
double oldY = pointerY.get();
pointerX.set(pointerX.get()
+ Math.cos((pointerR.get() - 90) * Math.PI/180)
* movement);
pointerY.set(pointerY.get()
+ Math.sin((pointerR.get() - 90) * Math.PI/180)
* movement);
if (checkCollisions)
handleCollision(oldX, oldY, pointerX.get(), pointerY.get());
pointerR.set(pointerR.get() + rotation);
}
});
感謝您的回答。我不認爲這解決了我在更大的地圖上遇到的問題,如果我刪除了旋轉 - 轉換,則問題會自行解決。另外,重點應始終在此節點或其子節點上,因爲目前沒有其他節點。 – Rouby