onKeyPressed函数在JavaFX中不起作用

时间:2016-07-30 18:02:34

标签: animation javafx

我正在尝试暂停PathTransition并使用键盘键P重新启动它。 而且我也试图使用向上/向下键增加和减少动画速度。

但是当我运行代码时,这些按钮似乎不起作用。我做错了什么?

    package exercise_15_29;

import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.input.KeyCode;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Exercise_15_29 extends Application {
    Group car = new Group();
    Circle wheel1 = new Circle(15,95,5);
    Circle wheel2 = new Circle(35,95,5);
    Polygon body1 = new Polygon();
    Rectangle body2 = new Rectangle(0.0,80.0,50,10);
    Line path = new Line(0,90,500,90);

    int speed = 4000;
    boolean play = true;

    @Override
    public void start(Stage primaryStage) {

        body1.getPoints().addAll(new Double[]{
           10.0, 80.0,
           20.0, 70.0,
           30.0, 70.0,
           40.0, 80.0
        });
        body1.setFill(Color.BLUE);
        body2.setFill(Color.SKYBLUE);
        path.setVisible (false);

        car.getChildren().addAll(wheel1,wheel2,body1,body2);

        PathTransition pt = new PathTransition();
        pt.setDuration(Duration.millis(speed));
        pt.setPath(path);
        pt.setNode(car);
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(false);

        pt.play();

        Pane root = new Pane();
        root.getChildren().add(car);
        root.getChildren().add(path);

        Scene scene = new Scene(root, 500, 100);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        root.setOnKeyTyped(e -> {
            if(e.getCode() == KeyCode.P) {
               if(play == true)
                  pt.stop();
               else
                  pt.play();
               play = !play;
            }
        });

        root.setOnKeyPressed(e -> {
            if(e.getCode() == KeyCode.UP)
               pt.setDuration(Duration.millis(++speed));
            else if(e.getCode() == KeyCode.DOWN) 
               pt.setDuration(Duration.millis(--speed));
        });
    }

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

}

1 个答案:

答案 0 :(得分:2)

此代码适用于我:

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Exercise_15_29 extends Application {
    Group car = new Group();
    Circle wheel1 = new Circle(15, 95, 5);
    Circle wheel2 = new Circle(35, 95, 5);
    Polygon body1 = new Polygon();
    Rectangle body2 = new Rectangle(0.0, 80.0, 50, 10);
    Line path = new Line(0, 90, 500, 90);

    int speed = 4000;
    boolean play = true;

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

    @Override
    public void start(Stage primaryStage) {

        body1.getPoints().addAll(new Double[]{
                10.0, 80.0,
                20.0, 70.0,
                30.0, 70.0,
                40.0, 80.0
        });
        body1.setFill(Color.BLUE);
        body2.setFill(Color.SKYBLUE);
        path.setVisible(false);

        car.getChildren().addAll(wheel1, wheel2, body1, body2);

        PathTransition pt = new PathTransition();
        pt.setDuration(Duration.millis(speed));
        pt.setPath(path);
        pt.setNode(car);
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(false);

        pt.play();

        Pane root = new Pane();
        root.getChildren().add(car);
        root.getChildren().add(path);

        Scene scene = new Scene(root, 500, 100);

        scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
            System.out.println("P");
            if (e.getCode() == KeyCode.P) {
                if (play)
                    pt.stop();
                else
                    pt.play();
                play = !play;
            }
        });
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

//        root.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
//            if (e.getCode() == KeyCode.P) {
//                if (play == true)
//                    pt.stop();
//                else
//                    pt.play();
//                play = !play;
//            }
//        });


        scene.setOnKeyPressed(e -> {
            System.out.println("UP");
            if (e.getCode() == KeyCode.UP)
                pt.setDuration(Duration.millis(++speed));
            else if (e.getCode() == KeyCode.DOWN)
                pt.setDuration(Duration.millis(--speed));
        });
//        root.setOnKeyPressed(e -> {
//            if(e.getCode() == KeyCode.UP)
//                pt.setDuration(Duration.millis(++speed));
//            else if(e.getCode() == KeyCode.DOWN)
//                pt.setDuration(Duration.millis(--speed));
//        });
    }

}

我将KeyTyped事件更改为KEY_PRESSED(我建议这样做),并根据https://stackoverflow.com/a/24126049/3291867使用scene.addEventFilter代替root.setOnKeyPressed,最后您可以& #39; t改变汽车的速度,你不能在播放之后或之后改变动画持续时间(据我所知),你可以使用AnimationTimer来实现这一点。