【问题标题】:JavaFX - Am I misunderstanding how to use KeyValues?JavaFX - 我是否误解了如何使用 KeyValues?
【发布时间】:2016-11-11 11:58:41
【问题描述】:
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 书籍的动画部分后玩的一些 sn-p,但我想知道为什么它不起作用。基本上,这只是应该让一个圆圈反复向右和向左移动。 KeyValue 'back' 应该把它带回开始,但它没有,圆圈转到最右边,然后立即产生回到它开始的地方。

我对 KeyValues 有什么误解,还是什么?这里有什么问题?

【问题讨论】:

  • 在第三个 KeyFrame 上,持续时间可能应该是 Duration.seconds(10)(即 10 秒的关键帧)而不是 5?

标签: java animation javafx


【解决方案1】:

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

如果要在动画的前 5 秒内将 enemy.translateXProperty() 属性从 0 设置为 600,并在接下来的 5 秒内将其设置为 0,则需要使用 Duration.seconds(10) 作为构造函数的参数backKeyFrame

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多