修改JavaFX定期添加对象



我有很多对象通过PauseTranition定期添加到场景中,我希望它们随着时间的推移逐渐消失,如何实现这一点,或者如何修改对象的属性我一般没有参考资料?

我发现这很有帮助:TimeLine(JavaFX 11(

总而言之:您需要在PauseTransition(或任何其他线程(中包含一个Timeline,并从head对象更新要更改的属性,该代码显示了如何更新一行的不透明度:

Line line = new Line(x1,y1,x2,y2);
double timeBetweenEachUpdate = .1d;//seconds
PauseTransition pt = new PauseTransition(Duration.seconds(timeBetweenEachUpdate));
pt.setOnFinished((e) ->{
Timeline timeline = new Timeline();
double fadeTime=1000.0d;//ms
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(fadeTime)),
//here you can put any property , any value you want it to change to
new KeyValue(line.opacityProperty(),0));
timeline.play(); 
pt.playFromStart();
});
pt.play();

最新更新