JavaFX - 我是否误解了如何使用 KeyValues?

标签 java animation javafx

KeyValue start = new KeyValue(enemy.translateXProperty(), 0);
KeyValue end = new KeyValue(enemy.translateXProperty(), 600);
KeyValue back = new KeyValue(enemy.translateXProperty(), 0);

KeyFrame startFrame = new KeyFrame(Duration.ZERO, start);
KeyFrame endFrame = new KeyFrame(Duration.seconds(5), end);
KeyFrame backFrame = new KeyFrame(Duration.seconds(5), back);

Timeline timeline = new Timeline(startFrame, endFrame, backFrame);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();

这只是我在读的 javafx 书的动画部分中玩弄的一些片段,但我想知道为什么它不起作用。基本上,这只是让一个圆圈反复向右和向左移动。 KeyValue“返回”应该将其带回到开头,但事实并非如此,圆圈转到最右边,然后立即返回到开始的位置。

我是否误解了 KeyValues 的某些内容,或者是什么?这里有什么问题吗?

最佳答案

Duration time parameter KeyFrame constructor的花费的时间相对于动画的开始,而不是相对于最后一个KeyFrame

如果您想在动画的前 5 秒内将 enemy.translateXProperty() 属性从 0 动画化到 600,并在接下来的 5 秒内将其动画化回 0,则需要使用Duration.seconds(10) 作为 back KeyFrame 构造函数的参数。

KeyFrame backFrame = new KeyFrame(Duration.seconds(10), back);

请注意,为了简单地添加反转动画,您还可以将 autoReverse 设置为 true:

KeyValue start = new KeyValue(enemy.translateXProperty(), 0);
KeyValue end = new KeyValue(enemy.translateXProperty(), 600);

KeyFrame startFrame = new KeyFrame(Duration.ZERO, start);
KeyFrame endFrame = new KeyFrame(Duration.seconds(5), end);

Timeline timeline = new Timeline(startFrame, endFrame);
timeline.setAutoReverse(true);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();

关于JavaFX - 我是否误解了如何使用 KeyValues?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38274728/

相关文章:

angularjs - 在Angular 1.4+中页面加载时ng-repeat交错动画

combobox - JAVAFX可编辑组合框: refresh after changing a value

java - 使用 Spring Security OAuth2 刷新 token 为 null

java - 使用 JAXB 操作 XML 注释

java - IntelliJ - com.esotericsoftware.kryonet 包不存在

python - 使用 Python Gizeh 构建动画

java - 配置 ant 以运行单元测试。图书馆应该在哪里?类路径应该如何配置?避免 ZipException

jquery - 基本的 jQuery SlideUp 和 SlideDown 让我抓狂!

java - 绘制简单的形状 -> JavaFX 的绘制方法和接口(interface)

java - 在自定义 JavaFX FXML 控件上添加特定节点列表