JavaFX第一人称鼠标中心

时间:2016-09-03 21:57:20

标签: javafx

我正在尝试学习在javafx中编程3D。我的目标是将光标锁定在中心或将其移动到中心以进行第一人称视图。

我做了一个鼠标移动事件,我将光标移动到屏幕中间,然后根据我移动鼠标的位置更改相机。问题是每当它将光标移回中心时,事件再次触发,反转摄像机角度的变化。任何人都知道一种方法,当它将光标移回中心时,它不会影响相机角度吗?

    scene.setOnMouseMoved(e -> {
        if (ignoreMouseMove) {
            ignoreMouseMove = false;
        } else {
            double cx = primaryStage.getX() + scene.getX() + sceneX / 2;
            double cy = primaryStage.getY() + scene.getY() + sceneY / 2;
            double dx = mouseX - e.getSceneX();
            double dy = mouseY - e.getSceneY();
            mouseX = e.getSceneX();
            mouseY = e.getSceneY();
            camAngleX += dx / 10;
            camAngleY -= dy / 10;
            ignoreMouseMove = true;
            moveMouse(cx, cy);
        }
    }

这是添加布尔检查后的... 我在其他地方定义了“ignoreMouseMove”。

function oddsAndEvens (arr) {
    var odd_count = 0,
        even_count = 0;

    for(i=0; i < arr.length; i++) {

        if(arr[i]%2==0) {
            event_count ++;

        }else if(arr[i]%2!=0){
            odd_count ++;
        }

    }

    if(even_count >= 3){
        console.log('even');
    }

    if(odd_count >= 3){
        console.log('odd');
    }

}

1 个答案:

答案 0 :(得分:0)

您只需要一个布尔检查,如:

boolean ignoreMouseMove = false;

scene.setOnMouseMoved(e -> {
    if(ignoreMouseMove) {
        ignoreMouseMove = false;
    } else {
        int screenWidth = (int) Screen.getPrimary().getBounds().getWidth();
        int screenHeight = (int) Screen.getPrimary().getBounds().getHeight();
        ignoreMouseMove = true;
        moveCursor(screenWidth / 2, screenHeight / 2);
        double dx = mouseX - e.getSceneX();
        double dy = mouseY - e.getSceneY();
        mouseX = e.getSceneX();
        mouseY = e.getSceneY();
        camAngleX += dx / 10;
        camAngleY -= dy / 10;
    }
});

如果它对你有所帮助,那么几个星期前我做的一个实现就可以了:

Robot mouseMover = new Robot(); //defined somewhere else

scene.setOnMouseMoved(event -> {
    if(ignoreMouseEvent) {
        ignoreMouseEvent = false;
        return;
    }

    mouseDeltaX += Math.round(event.getScreenX() - (stage.getX() + (stage.getWidth() / 2.0))) / 5.0;
    mouseDeltaY += -Math.round((event.getScreenY() - (stage.getY() + (stage.getHeight() / 2.0)))) / 5.0;

    ignoreMouseEvent = true;
    mouseMover.mouseMove((int) (stage.getX() + (stage.getWidth() / 2.0)), (int) (stage.getY() + (stage.getHeight() / 2.0)));
});