JavaFX - 时间轴动画对象不显示?

我确定'm missing something obvious here. I'我试图制作一辆汽车的动画,它从左到右穿过一扇窗户,当它到达那里时绕着右侧缠绕 . 用户应该能够按上/下来调整动画的速度 . 当我使用 PathTransition 对象时,我有动画,但发现你无法调整 PathTransitionDuration ,所以我在 Timeline 中重新编写它 .

但是,对于 Timeline ,我希望是一个简洁的代码片段:

public class Project12 extends Application {

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

@Override
public void start(Stage primaryStage) {     
    //Create Pane to hold the car animation
    Pane pane = new Pane();

    //Create the RaceCarPane
    RaceCarPane raceCar = new RaceCarPane();
    pane.getChildren().add(raceCar); //Adds the race car to the main pane

    //Create the VBox to hold components
    VBox displayPane = new VBox();
    displayPane.getChildren().addAll(pane, userInstructions, btnPause);
    displayPane.setSpacing(15);
    displayPane.setAlignment(Pos.CENTER);

    //Create scene for display and add the display pane
    Scene scene = new Scene(displayPane);

    //Add the scene to the stage and display
    primaryStage.setTitle("Project 12");
    primaryStage.setResizable(false); //disable resizing of the window
    primaryStage.setScene(scene);
    primaryStage.show();

}

而RaceCarPane:

public class RaceCarPane extends Pane {

    //Declare origin for determining polygon point locations
    private double originX = 10;
    private double originY = getHeight() - 10;
    private Timeline carAnimation;

    //Set the Timeline for the car in constructor method
    public RaceCarPane() {
        carAnimation = new Timeline(
                new KeyFrame(Duration.millis(100), e -> moveCar()));
        carAnimation.setCycleCount(Timeline.INDEFINITE);
        carAnimation.play();
    }

    private void paint() {

        //Create a polygon for the body 
        Polygon body = new Polygon();
        body.setFill(Color.BLUE);
        body.setStroke(Color.DARKBLUE);


        //Add points to the body
        ObservableList<Double> bodyList = body.getPoints();

        /*(code omitted, just adding coordinates to the ObservableList for all parts. 
        I don't believe the bug is here since it displayed when I was using a PathTransition animation)*/

        //Add to pane
        getChildren().addAll(body, roof, frontWheel, rearWheel);
    }

    public void setOrigin (double x, double y) {
        this.originX = x;
        this.originY = y;
    }

    @Override
    public void setWidth(double width) {
        super.setWidth(width);
        paint();
    }

    @Override
    public void setHeight(double height) {
        super.setHeight(height);;
        paint();
    }

    public void moveCar() {
        //Check that car is in bounds
        if(originX <= getWidth()) {
            originX += 10;
            paint();
        }
        else {
            originX = 0;
            paint();
        }

编辑:Per @Jai的评论如下,我的解决方案是恢复到 PathTransition 对象并使用 RateProperty 绑定到 SimpleDoubleProperty . 虽然可能不是该项目所寻求的解决方案,但它可以解决问题,所以我很开心!

回答(2)

3 years ago

@swpalmer说你不应该反复添加它是正确的 .

除此之外,您绝对可以使用Animation.rateProperty()PathTransition extends Animation )来使用 PathTransition .

另外,来自PathTransition

此Transition创建一个跨越其持续时间的路径动画 . 沿路径的转换是通过更新节点的translateX和translateY变量来完成的,如果orientation定期设置为OrientationType.ORTHOGONAL_TO_TANGENT,则旋转变量将更新 .

因此,如果您使用 Timeline ,您还应该设置整个节点的 translateXtranslateY (即 RaceCarPane )来执行动画;试图反复添加多边形绝对是错误的方法 . 如果您在使用 PathTransition 时也添加了很多多边形,那么您可能也没有做到这一点 . 即使动画在视觉上是正确的,也并不总是意味着它是正确完成的 .

3 years ago

看起来你错过了paint方法中的右大括号 .

有一些事情是“错误的” . 首先,您不应每次想要移动汽车时创建新对象并将其添加到场景图中 . 你也忽略了你如何使用originX,originY .

我建议你用一个小组来 grab 汽车的各个部分 . 将它添加到RaceCarPane并使用组对象上的变换来移动它 .

如果您基于AnimationTimer而不是时间轴,这可能会更容易,特别是如果您希望能够动态调整汽车的速度 .