JavaFX动画使用循环吗?

时间:2019-02-28 16:51:10

标签: java javafx

我正在尝试建立像游戏这样的太空入侵者。 我已经画了一个正方形,我想通过使用循环和UPDATE STGOrderDetail SET [Order ID] = REPLACE([Order ID],'USA','US-') WHERE [Ship Mode] LIKE 'Standard Class' AND [Order ID] LIKE 'USA%' 来将其逐渐向下移动。但是,正方形会立即被绘制。我知道有可以使用的动画路径,但我想保持较低的水平,而只使用坐标系。

是否可以使用这样的循环制作时间轴动画?

thread.sleep

2 个答案:

答案 0 :(得分:1)

这是一个糟糕的实现。我可能会使用AnimationTimer。这是通过Timeline完成的。它基本上在向右或向左移动。如果您击中了右边界或左边界,则放下然后沿相反方向移动。

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication343 extends Application
{
    int invaderWidth = 30;
    int invaderHeight = 10;
    int gapBetweenInvaderX = 5;
    int gapBetweenInvaderY = 5;
    int locationTrackerX;
    int locationTrackerY;

    int screenWidth = 300;
    int screenHeight = 400;

    double timeBetweenFrames = .25;

    boolean direction = true;

    Timeline timeline;

    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();

        locationTrackerX = (screenWidth - (invaderWidth * 6 + gapBetweenInvaderX * 5)) / 2;
        locationTrackerY = (screenHeight - (invaderHeight * 6 + gapBetweenInvaderY * 5)) / 7;

        List<Rectangle> invaders = new ArrayList();
        for (int i = 0; i < 36; i++) {
            Rectangle rectangle = new Rectangle(locationTrackerX, locationTrackerY, invaderWidth, invaderHeight);
            rectangle.setFill(Color.YELLOW);
            invaders.add(rectangle);
            System.out.println(locationTrackerX);

            locationTrackerX += invaderWidth + gapBetweenInvaderX;
            if ((i + 1) % 6 == 0) {
                locationTrackerX = (screenWidth / 2) - ((invaderWidth * 6 + gapBetweenInvaderX * 5) / 2);
                locationTrackerY += invaderHeight + gapBetweenInvaderY;
            }
        }

        timeline = new Timeline(new KeyFrame(Duration.seconds(timeBetweenFrames), (event) -> {
            //Check to see if invader hits bounds
            Optional<Rectangle> hitRightOptional = invaders.stream().filter(invader -> invader.getBoundsInLocal().getMaxX() >= pane.getWidth()).findFirst();
            Optional<Rectangle> hitLeftOptional = invaders.stream().filter(invader -> invader.getBoundsInLocal().getMinX() <= 0).findFirst();
            //Move invaders
            if (hitRightOptional.isPresent()) {
                invaders.forEach((tempInvader) -> tempInvader.setY(tempInvader.getY() + 10));
                timeline.stop();
                PauseTransition pause = new PauseTransition(Duration.seconds(timeBetweenFrames));
                pause.setOnFinished((pauseEvent) -> {
                    invaders.forEach(invader -> invader.setX(invader.getX() - 10));
                    timeline.play();
                });
                pause.play();

                direction = false;
            }
            else if (hitLeftOptional.isPresent()) {
                invaders.forEach((tempInvader) -> tempInvader.setY(tempInvader.getY() + 10));
                timeline.stop();
                PauseTransition pause = new PauseTransition(Duration.seconds(timeBetweenFrames));
                pause.setOnFinished((pauseEvent) -> {
                    invaders.forEach(invader -> invader.setX(invader.getX() + 10));
                    timeline.play();
                });
                pause.play();

                direction = true;
            }
            else {
                if (direction) {
                    invaders.forEach(invader -> invader.setX(invader.getX() + 10));
                }
                else {
                    invaders.forEach(invader -> invader.setX(invader.getX() - 10));
                }
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);

        Button btn = new Button();
        btn.setText("Start Game");
        btn.setOnAction((ActionEvent event) -> {
            timeline.play();
            btn.setDisable(true);
        });

        pane.getChildren().addAll(invaders);
        pane.setPrefSize(screenWidth, screenHeight);

        VBox root = new VBox(pane, new StackPane(btn));
        Scene scene = new Scene(root, screenWidth, screenHeight);

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

答案 1 :(得分:-1)

已解决:

       Task task = new Task<Void>() {
        @Override public Void call() throws InterruptedException {

            for (int i = 1; i <= 800; i=i+10) {
                Thread.sleep(25);
                //updateProgress(i, max);
                r.setTranslateY(i);
            }
            return null;
        }
    };
    new Thread(task).start();