0

For example, I am using javaFx scene and I add a plane object and then add the group with required missle images and then add scene. Now when I add an event filter on the scene of moving a missle then remove it just after, the scene still responds to keyboard presses in moveMissle() event filters when it runs. Also, if you add event filters it does not yet does not remove them using the remvoveEventFilter method. Shouldn't it remove it because I coded to remove the event Filter so it does not respond to any keys? Thank you.

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Plane plane = new Plane();
        Group group = new Group(plane.getPlaneImage(), plane.getMissleZero().getMissleImage(), plane.getMissleOne().getMissleImage(), plane.geMissleTwo().getMissleImage(), plane.getMissleThree().getMissleImage(), plane.getMissleFour().getMissleImage());
        Scene scene = new Scene(group, 700, 700);
        scene.setFill(Color.BLACK);
        scene.addEventFilter(KeyEvent.KEY_PRESSED, plane.getMissleZero().moveMissle());
        scene.removeEventFilter(KeyEvent.KEY_PRESSED, plane.getMissleZero().moveMissle());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
  • 1
    How is `moveMissile()` implemented? – fabian Sep 03 '18 at 11:41
  • It is implemented as public EventHandler moveMissle(){// code to move Missle using Animation Timer}. The Animation Timer is a field of the Missle class where the code is put. –  Sep 04 '18 at 00:33
  • The signature of the method is unimportant. The kind of object you return is important though. I'm pretty sure it's responsible for the issue (the exact `handle` implementation is not really important; the important part is the type of object returned and how it's created). – fabian Sep 04 '18 at 01:23
  • Sorry, the moveMissle() returns the following: return key = (KeyEvent keyEvent) -> {// code with method to start the bullet movement animation timer}. –  Sep 04 '18 at 01:34
  • Take a look at this question: https://stackoverflow.com/questions/24095875/is-there-a-way-to-compare-lambdas Your code won't work as long as you don't return the same instance of the event handler for both calls of `moveMissile`. This would require you to store the handler in a field. – fabian Sep 04 '18 at 01:39
  • Just to clarify, you are suggesting to put plane.getMissleZero.moveMisslle() as a field of a KeyEvent Handler then call the same field to add and remove it? Also, sorry I don't understand your suggested question... what are lambdas? –  Sep 04 '18 at 01:47
  • The exact implementation details don't matter, but you need to fix the fact that even you use the same lambda expression, executing it twice won't yield the same instance or instances that yield `true` if compared using `equals`. You could add a field to the class containing `moveMissile` and store the object in a field to always return the same object for all calls. Alternatively you could invoke the method only once in `start` and store the result in a variable to ensure the same object is passed to `addEventFilter`/`removeEventFilter`. – fabian Sep 04 '18 at 01:55
  • By creating a field: EventHandler moveMissleEventHandler = plane.getMissleZero().moveMissle() in the main class (in the original post) it has successfully recognized both are equal and removes the event handler. Thank you so much for your time, commitment, effort and consideration. –  Sep 04 '18 at 03:06
  • Nevermind, I noticed I was using removeEventHandler instead of removeEventFilter...! –  Sep 08 '18 at 21:23

1 Answers1

0

The main reason of my inability to add and remove the exact same eventFilter is from incorrectly using eventHandler when needing to use eventFilter. The comments in the original post, I believe, also contirbuted to the problem.