JavaFX 动画不工作
JavaFX animation not working
我正在尝试制作一个对象在屏幕上移动的应用程序,仅更改其 x 值。我没有收到任何错误,但是当我 运行 它时它不起作用。对象正在显示,但矩形没有动画。我不知道该怎么做
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private BorderPane root;
private Stage stage;
private Scene scene;
private AnchorPane anchorPane;
private Rectangle player;
private Circle circles;
private Button up, down;
private Timeline timer;
@Override
public void start(Stage primaryStage) {
stage = primaryStage;
root = new BorderPane();
scene = new Scene(root, 600, 500); //width and height of application
stage.setScene(scene);
stage.setTitle("Hoop runner"); //text for the title bar of the window
scene.getStylesheets().add("hoop.css");
anchorPane = new AnchorPane();
anchorPane.setMaxHeight(500);
anchorPane.setMinHeight(500);
anchorPane.setMinWidth(600);
anchorPane.setMaxWidth(600);
player = new Rectangle();
circles = new Circle();
up = new Button("˄");
down = new Button("˅");
root.setCenter(new VBox(new VBox(circles),
new HBox(player)));
root.setCenter(anchorPane);
player.setX(5);
player.setY(380);
player.setHeight(80);
player.setWidth(120);
anchorPane.getChildren().add(player);
timer = new Timeline(Animation.INDEFINITE);
timer.setCycleCount(Animation.INDEFINITE);
timer.getKeyFrames().add(new KeyFrame(Duration.INDEFINITE, event -> maverick()));
timer.play();
stage.show();
}
private void maverick() {
timer.play();
System.out.println("hi");
player.setX(player.getX() + 2);
timer.play();
}
public static void main(String[] args) {
launch(args);
}
}
- 如果要定位或动画节点,请使用
setTranslateX
而不是 setX
。
- 将
timer = new Timeline(Animation.INDEFINITE)
更改为 timer = new Timeline()
。
- 将
new KeyFrame(Duration.INDEFINITE, event -> maverick())
更改为new KeyFrame(Duration.millis(500), event -> maverick())
(例如)。
关键帧必须有一个时间点。如果像示例一样使用 500 毫秒,则 KeyFrame 的 EventHandler 将在 500 毫秒后被调用。由于您的 TimeLine 的 cycleCount 为 INDEFINITE,它将每 500 毫秒循环并执行一次 KeyFrame。
我正在尝试制作一个对象在屏幕上移动的应用程序,仅更改其 x 值。我没有收到任何错误,但是当我 运行 它时它不起作用。对象正在显示,但矩形没有动画。我不知道该怎么做
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private BorderPane root;
private Stage stage;
private Scene scene;
private AnchorPane anchorPane;
private Rectangle player;
private Circle circles;
private Button up, down;
private Timeline timer;
@Override
public void start(Stage primaryStage) {
stage = primaryStage;
root = new BorderPane();
scene = new Scene(root, 600, 500); //width and height of application
stage.setScene(scene);
stage.setTitle("Hoop runner"); //text for the title bar of the window
scene.getStylesheets().add("hoop.css");
anchorPane = new AnchorPane();
anchorPane.setMaxHeight(500);
anchorPane.setMinHeight(500);
anchorPane.setMinWidth(600);
anchorPane.setMaxWidth(600);
player = new Rectangle();
circles = new Circle();
up = new Button("˄");
down = new Button("˅");
root.setCenter(new VBox(new VBox(circles),
new HBox(player)));
root.setCenter(anchorPane);
player.setX(5);
player.setY(380);
player.setHeight(80);
player.setWidth(120);
anchorPane.getChildren().add(player);
timer = new Timeline(Animation.INDEFINITE);
timer.setCycleCount(Animation.INDEFINITE);
timer.getKeyFrames().add(new KeyFrame(Duration.INDEFINITE, event -> maverick()));
timer.play();
stage.show();
}
private void maverick() {
timer.play();
System.out.println("hi");
player.setX(player.getX() + 2);
timer.play();
}
public static void main(String[] args) {
launch(args);
}
}
- 如果要定位或动画节点,请使用
setTranslateX
而不是setX
。 - 将
timer = new Timeline(Animation.INDEFINITE)
更改为timer = new Timeline()
。 - 将
new KeyFrame(Duration.INDEFINITE, event -> maverick())
更改为new KeyFrame(Duration.millis(500), event -> maverick())
(例如)。
关键帧必须有一个时间点。如果像示例一样使用 500 毫秒,则 KeyFrame 的 EventHandler 将在 500 毫秒后被调用。由于您的 TimeLine 的 cycleCount 为 INDEFINITE,它将每 500 毫秒循环并执行一次 KeyFrame。