SetOnKeyPressed无法正常工作(或者根本无法工作)

时间:2019-04-15 06:36:57

标签: javafx switch-statement case

我写了一个游戏,一切正常,但是当我创建一个控件时,我决定使用Switch-case。后来我编写了代码,但最重要的是scene.setOnKeyPressed无法正常工作(更确切地说,我给出了一些异常,我认为这些异常与问题无关)。有趣的是,如果我将其更改为“ board”(组),则程序将启动,但在按下时将无法运行。我不擅长JavaFX,可能马上就做错了,但我寻求帮助。

我只显示某种类型的代码(因为该代码在不使用scene.setOnKeyPressed的情况下可以工作)

///此主要代码,向上,向下,向右,向左,射击-布尔值 //公共小组委员会;

       scene1.setOnKeyReleased((KeyEvent event) -> {
       switch (event.getCode()){
             case DIGIT1: goup = true; break;
             case DIGIT2: godown = true; break;
             case DIGIT3: goright = true; break;
             case DIGIT4: goleft = true; break;
             case DIGIT5: if(!shoot){
                 /*some code*/
             }
              /*some code*/
         }
             });

    scene1.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override 
    public void handle(KeyEvent event){
        switch (event.getCode()){
          case DIGIT1: goup = true; break;
          case DIGIT2: godown = true; break;
          case DIGIT3: goright = true; break;
          case DIGIT4: goleft = true; break;
         case DIGIT5: shoot = false; break;   
    }
    }
    });

//或带有“木板”

      board.setOnKeyReleased((KeyEvent event) -> {
       switch (event.getCode()){
             case DIGIT1: goup = true; break;
             case DIGIT2: godown = true; break;
             case DIGIT3: goright = true; break;
             case DIGIT4: goleft = true; break;
             case DIGIT5: if(!shoot){
                 /*some code*/
             }
              /*some code*/
         }
             });

 board.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override 
    public void handle(KeyEvent event){
        switch (event.getCode()){
          case DIGIT1: goup = true; break;
          case DIGIT2: godown = true; break;
          case DIGIT3: goright = true; break;
          case DIGIT4: goleft = true; break;
          case DIGIT5: shoot = false; break;

    }
    }
    });

如果我编写的代码没有此块,那么一切都会正常工作(当然不控制字符),但是有了它,就会出错:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
        at testgame.Testgame.start(Testgame.java:59)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more

UPD。我知道NPE错误只有在进入scene1.setOnKeyPressed行之后才会消失。在其他情况下,一切都会开始并按预期进行。显然我需要通过另一个控件编写一个字符控件,因为我不知道如何更正此错误

0 个答案:

没有答案