3

タイムラインでプライマリ ステージを振ることは可能で、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);
4

2 に答える 2

3

このデモの完全なコード ( Gistとしても入手可能):

ここに画像の説明を入力

public class ShakingStage extends Application {

    public Stage primaryStage;
    boolean xyState= true;

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

    @Override
    public void start(Stage stage) throws Exception {
        primaryStage= stage;
        Button shakeButton= ButtonBuilder.create()
                .text("shake it")
                .onAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        TimelineBuilder.create()
                                .keyFrames(new KeyFrame(Duration.seconds(0.2), new EventHandler<ActionEvent>() {
                                    @Override
                                    public void handle(ActionEvent actionEvent) {
                                        if (xyState) {
                                            primaryStage.setX(primaryStage.getX() + 10);
                                            primaryStage.setY(primaryStage.getY() + 10);
                                        } else {
                                            primaryStage.setX(primaryStage.getX() - 10);
                                            primaryStage.setY(primaryStage.getY() - 10);
                                        }
                                        xyState= !xyState;
                                    }
                                }))
                                .autoReverse(true)
                                .cycleCount(Timeline.INDEFINITE)
                                .build().play();
                   }
                })
                .build();

        Scene scene= SceneBuilder.create()
                .root(StackPaneBuilder.create()
                        .children(shakeButton)
                        .build()
                )
                .width(300)
                .height(400)
                .build();

        stage.setScene(scene);
        stage.show();
    }
}
于 2013-05-20T19:06:30.177 に答える
3

これを試して..

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();
    }
于 2013-05-20T07:08:21.890 に答える