JavaFX应用的转换似乎阻止了KeyEvents

时间:2012-11-04 19:19:49

标签: javafx-2

关于:我有一个面板,显示由多边形和线条构成的地图。在这张地图中,我应用了变换并提供了一种四处走动(第一人称)的方法,通过旋转和移动地图来移动地图。

应用于地图的转换似乎阻止(或至少减速)处理过的KeyEvent。有时,即使我发布了密钥,应用程序也会继续旋转。

此外,我注意到旋转变换对此效果的影响更大。

碰撞检测对此没有影响(因为此时已禁用)。

由于应用程序的结构,我将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);
    }
});

1 个答案:

答案 0 :(得分:0)

你在setOnKeyPressed上调用了什么节点?查看此方法的javadoc:

Defines a function to be called when this Node or its child Node 
has input focus and a key has been pressed. The function is called 
only if the event hasn't been already consumed during its capturing
or bubbling phase.

确保将侦听器添加到场景中(如果尚未调用),并且没有调用事件对象上的consume方法。