Reputation: 41
I am trying to make the elements of a Text
array fade away one by one in a for
loop, but for some reason they fade away together.
Here is the code:
for (int i = 0; i < citiesText.length; i++) {
if (i != cityNum) {
PauseTransition pause = new
PauseTransition(Duration.millis(3000));
FadeTransition ft = new FadeTransition(Duration.millis(3000), citiesText[i]);
SequentialTransition st = new SequentialTransition(pause, ft);
ft.setFromValue(1);
ft.setToValue(0);
st.play();
}
}
Upvotes: 4
Views: 96
Reputation: 3563
Your PauseTransition
defines a pause of three seconds (btw Duration.seconds(3)
would have been clearer). You create all Transitions with the same delay, and then you start them at once. Thus they all wait three seconds and then Fade for three seconds.
Note there is no need to create a PauseTransition
, a FadeTransition
and then combine them into a SequentionalTransition
, the Animation
superclass has a setDelay()
which you can use to delay the execution of the animation.
The easiest change for your code would be to keep a delay counter and increment it each time:
double delay = 0d;
for (....) {
if (....) {
FadeTransition fade = ....;
....
fade.setDelay(Duration.seconds(delay));
delay += 3d;
fade.play();
}
}
Upvotes: 0
Reputation: 1192
I did the same thing you are doing but with Cards.
I got it to work by nesting SequentialTransition
s.
I did something similar to this:
private SequentialTransition slideshow = new SequentialTransition();
for(int i = 0; i < citiesText.length; i++){
SequentialTransition seq = new SequentialTransition();
FadeTransition fade = new FadeTransition(Duration.millis(2000), citiesText[i]);
fade.setFromValue(1);
fade.setToValue(0);
PauseTransition stop = new PauseTransition(Duration.millis(3000));
seq.getChildren().addAll(fade, stop);
slideshow.getChildren().add(seq);
}
slideshow.play();
As you can see I nested teh SequentialTransition
s so that each citiesText
has its own, but they play in order because of the outer SequentialTransition
Upvotes: 2