JavaFXを使ってカードゲームを作っています。カードをImageViewで作成し、TranslateTransitionクラスを使ってアニメーションさせました。全員にカードが行きわたったところで、1秒間、間をおいてゲームを始めるという振る舞いをさせたいのですがうまくいきません。Transitionを行った後、Thread.sleep()やwait()などで1秒間スレッドを止めると、スレッドを再開した時、1秒後の偏移状態が表現されてしまいます。(カクっとなったり、すでにアニメーションが終わっていたり。)
なので、まず、Transitionクラスのplayメソッドが終わってからスレッドを止めればいいんだと考えたのですが、それがうまくいきません。ImageViewではありませんが下記のようなクラスを作ってみました。
lang
1package jp.ocn; 2 3import javafx.animation.TranslateTransition; 4import javafx.application.Application; 5import javafx.beans.property.BooleanProperty; 6import javafx.beans.property.SimpleBooleanProperty; 7import javafx.event.ActionEvent; 8import javafx.event.EventHandler; 9import javafx.scene.Scene; 10import javafx.scene.control.Button; 11import javafx.scene.control.Label; 12import javafx.scene.layout.AnchorPane; 13import javafx.stage.Stage; 14import javafx.util.Duration; 15 16public class TransitionTestMain extends Application { 17 BooleanProperty flagProperty = new SimpleBooleanProperty(false); 18 int abs = 1; 19 20 @Override 21 public void start(Stage primaryStage) throws Exception { 22 23 Button start_button = new Button("Transition Start"); 24 Label status_label = new Label("Ready"); 25 Label transition_label = new Label("Transition Label"); 26 27 start_button.setLayoutX(250); 28 start_button.setLayoutY(100); 29 status_label.setLayoutX(270); 30 status_label.setLayoutY(50); 31 32 AnchorPane root = new AnchorPane(); 33 root.getChildren().add(start_button); 34 root.getChildren().add(transition_label); 35 root.getChildren().add(status_label); 36 37 start_button.setOnAction(new EventHandler<ActionEvent>() { 38 39 @Override 40 public void handle(ActionEvent event) { 41 42 status_label.setText("runnning"); 43 44 TranslateTransition transition = new TranslateTransition( 45 new Duration(500), transition_label); 46 transition.setToX(500*abs); 47 transition.setToY(375*abs); 48 49 transition.setOnFinished(new EventHandler<ActionEvent>() { 50 51 @Override 52 public void handle(ActionEvent event) { 53 54 flagProperty.set(true); 55 } 56 57 }); 58 59 transition.play(); 60 61// while(!flagProperty.get()){ 62// ここでスレッドの進行を止めようと考えました 63// } 64 65 abs*=-1; 66 status_label.setText("Succeeded"); 67 flagProperty.set(false); 68 } 69 }); 70 71 Scene scene = new Scene(root, 600, 400); 72 primaryStage.setScene(scene); 73 primaryStage.centerOnScreen(); 74 primaryStage.show(); 75 76 } 77 78 public static void main(String[] args) { 79 launch(args); 80 } 81 82}
やってみて、Transitionは非同期処理ではないのだと分かったのですが、では、どのように実装をしたらいいのでしょうか?
transition.play()の後に、ボタン一つのダイアログボックスを表示させ、進行ごとにプレイヤーにクリックしてもらえば、仕様通りにはなりますが、ゲームとして不自然だと思います。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/01/07 10:31
2015/01/09 07:19
2015/01/09 07:48
2015/01/12 11:06
2015/01/14 06:57
2015/01/15 02:17
2015/01/16 05:54
2015/01/16 06:16