javaFX中的摇晃舞台

javaFX中的摇晃舞台

是否可以使用 Timeline 晃动初级阶段,然后使用 XTimeline 和 YTimeLine?

    final Timeline Xtimeline = new Timeline();//for Animate On X'-Stage
    Xtimeline.setCycleCount(Timeline.INDEFINITE);
    Xtimeline.setAutoReverse(true);
    final Timeline Ytimeline = new Timeline();//for Animate On Y'-Stage
    Ytimeline.setCycleCount(Timeline.INDEFINITE);
    Ytimeline.setAutoReverse(true);

最佳答案

试试这个..

int x = 0;
int y = 0;

public void shakeStage() {
        Timeline timelineX = new Timeline(new KeyFrame(Duration.seconds(0.1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (x == 0) {
                    primaryStage.setX(primaryStage.getX() + 10);
                    x = 1;
                } else {
                    primaryStage.setX(primaryStage.getX() - 10);
                    x = 0;
                }
            }
        }));

        timelineX.setCycleCount(Timeline.INDEFINITE);
        timelineX.setAutoReverse(false);
        timelineX.play();


        Timeline timelineY = new Timeline(new KeyFrame(Duration.seconds(0.1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (y == 0) {
                    primaryStage.setY(primaryStage.getY() + 10);
                    y = 1;
                } else {
                    primaryStage.setY(primaryStage.getY() - 10);
                    y = 0;
                }
            }
        }));

        timelineY.setCycleCount(Timeline.INDEFINITE);
        timelineY.setAutoReverse(false);
        timelineY.play();
    }

关于javafx-2 - javaFX中的摇晃舞台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16634960/

10-13 08:36