在不同的类中设置KeyHandler

时间:2019-04-11 11:13:04

标签: java object javafx

我想为我的蛇游戏添加一个用JavaFX制作的KeyHandler。

Main.java

public class App extends Application {

private static final int WIDTH = 450;
private static final int HEIGHT = 450;

private GameLoop loop;
private Grid grid;
private GraphicsContext context;

@Override
public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();
    Canvas canvas = new Canvas(WIDTH, HEIGHT);
    context = canvas.getGraphicsContext2D();

    canvas.setFocusTraversable(true);
    canvas.setOnKeyPressed(new KeyHandler());;

    reset();

    root.getChildren().add(canvas);

    Scene scene = new Scene(root);
    primaryStage.setResizable(false);
    primaryStage.setTitle("Snake");
    primaryStage.setOnCloseRequest(e -> System.exit(0));
    primaryStage.setScene(scene);
    primaryStage.show();

    (new Thread(loop)).start();
}

private void reset() {
    grid = new Grid(WIDTH, HEIGHT);
    loop = new GameLoop(grid, context);
    Painter.paint(grid, context);
}

public static void main(String[] args) {
    launch(args);
}

}

我想使用方法“ canvas.setOnKeyPressed(new KeyHandler());”设置不同的键来像这样移动蛇:

Keyhandler.java

public class KeyHandler implements EventHandler<KeyEvent> {
    private GameLoop loop;
    private Grid grid;

@Override
public void handle(KeyEvent event) {
    Snake snake = grid.getSnake();
    if (loop.isKeyPressed()) {
        return;
    }
    switch (event.getCode()) {
    case W:
        snake.setUp();
        break;
    case DOWN:
        snake.setDown();
        break;
    case LEFT:
        snake.setLeft();
        break;
    case RIGHT:
        snake.setRight();
        break;
//      case ENTER:
//          if (loop.isPaused()) {
//              reset();
//              (new Thread(loop)).start();
//          }
        }
    };

}

启动应用程序时,每按一次键,都会出现nullPointer异常。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at snake.controller.KeyHandler.handle(KeyHandler.java:15)
    at snake.controller.KeyHandler.handle(KeyHandler.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:4058)
    at javafx.scene.Scene$KeyHandler.access$1500(Scene.java:4004)
    at javafx.scene.Scene.processKeyEvent(Scene.java:2121)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2595)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:547)
    at com.sun.glass.ui.View.notifyKey(View.java:971)

0 个答案:

没有答案