AlmasB / FXGL

Java / JavaFX / Kotlin Game Library (Engine)
http://almasb.github.io/FXGL/
MIT License
4.45k stars 556 forks source link

Regarding the issue of user input #1374

Closed huayunliufeng closed 5 months ago

huayunliufeng commented 6 months ago

There is now a scenario where the binding input by the user is not completed in the initInput method, but is implemented in the game control logic. For example, in a custom new game menu, there is code like this: FXGL.getGameController().startNewGame(); ChessController.getInstance().startNewGame(); In the startNewGame method, I bound input events:

eventHandler = event -> {
    if (event.getButton() == MouseButton.PRIMARY) {
        primaryClick();
    } else if (event.getButton() == MouseButton.SECONDARY) {
        secondaryClick();
    }
};
getInput().addEventHandler(MouseEvent.MOUSE_CLICKED, eventHandler);

Now, if I press ESC in the game and select to start a new game, the events bound to it will not be cleared, and I will be prompted to have the same event binding. May I know where the problem lies with me?

huayunliufeng commented 6 months ago

And, call getInput(). clearAll(); The method seems to have little effect.

AlmasB commented 6 months ago

Hi,

If you are adding JavaFX event handlers, then you can remove them in the same way.

getInput().removeEventHandler(...);
huayunliufeng commented 5 months ago

Yes, my problem has been resolved