简体   繁体   English

使用JavaFX的动画看起来很不稳定

[英]Animation using JavaFX looks choppy

I've been trying to maka a JavaFX application that moves a square to the right when the mouse is clicked. 我一直在尝试制作一个JavaFX应用程序,该应用程序在单击鼠标时向右移动一个正方形。 I'm using TranslateTransition to achieve this. 我正在使用TranslateTransition实现此目的。 The animation looks extremely choppy and I can't seem to figure out why. 动画看起来非常不稳定,我似乎无法弄清楚为什么。 Here is the code: 这是代码:

package main;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Pane root = new Pane();
            Rectangle player = new Rectangle(30,30, Color.rgb(242, 0, 0));
            player.relocate(100, 100);
            root.getChildren().add(player);
            Scene scene = new Scene(root,1280,720);
            movePlayerOnMouseClick(scene, player, createTranslateTransition(player));
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

    private TranslateTransition createTranslateTransition(Rectangle o) {
        final TranslateTransition transition = new TranslateTransition(Duration.seconds(1), o);
        transition.setOnFinished(new EventHandler<ActionEvent>() {
              @Override public void handle(ActionEvent t) {
                  o.setX(o.getTranslateX());
                  o.setY(o.getTranslateY());
              }
         });
         return transition;
     }

     private void movePlayerOnMouseClick(Scene scene, Rectangle o, final TranslateTransition transition){
         scene.setOnMousePressed(new EventHandler<MouseEvent>() {
             @Override public void handle(MouseEvent event) {
                 transition.setToX(o.getX() + 10 * Math.cos(Math.toRadians(0)));
                 transition.setToY(o.getY() + 10 * Math.sin(Math.toRadians(0)));
                 transition.playFromStart();
              }
          });
      }
}

Im using Java 8. 我正在使用Java 8。

The TranslateTransition performs the animation by updating the translateX and translateY properties of the node. TranslateTransition通过更新节点的translateXtranslateY属性来执行动画。 These are different to the x and y properties of the Rectangle (the rectangle is positioned first by looking at its x and y properties, and then applying its transformations, including the translation). 这些与Rectanglexy属性不同(首先通过查看矩形的xy属性,然后应用其转换(包括平移)来定位矩形。

So in the onFinished handler, you are causing the rectangle to jump to the location specified by the translation, with the translation still applied after that. 因此,在onFinished处理程序中,您将导致矩形跳转到翻译指定的位置,此后仍然应用翻译。 If you want to update the coordinates from the translation, you should add the translation to the coordinates, and then set the translation to zero: 如果要从平移更新坐标,则应将平移添加到坐标,然后将平移设置为零:

    transition.setOnFinished(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent t) {
              o.setX(o.getX() + o.getTranslateX());
              o.setY(o.getY() + o.getTranslateY());
              o.setTranslateX(0);
              o.setTranslateY(0);
          }
     });

and then you probably just want 然后你可能只想

 private void movePlayerOnMouseClick(Scene scene, Rectangle o, final TranslateTransition transition){
     scene.setOnMousePressed(new EventHandler<MouseEvent>() {
         @Override public void handle(MouseEvent event) {
             transition.setToX(10 * Math.cos(Math.toRadians(0)));
             transition.setToY(10 * Math.sin(Math.toRadians(0)));
             transition.playFromStart();
          }
      });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM