gpt4 book ai didi

JavaFX Timeline/KeyFrame - 传递的值不正确

转载 作者:行者123 更新时间:2023-11-29 04:06:05 28 4
gpt4 key购买 nike

编辑:Java/FX 版本:13.0.2

集成环境:Eclipse 4.13.0

Tracker 和 changePerFrame 值在类级别上声明为私有(private) double 变量。

目标:创建一个时间轴,使用 Canvas 和图像制作一个简短的动画序列。图像将从屏幕外的 X/Y 位置和增加的尺寸开始。然后图像会缩小并进入视野,形成一种缩小?/缩小?效果。

问题:在循环内创建新的关键帧时,未传递正确的值。使用 println 在循环内打印时的值是正确的,但在“(evt -> swordAnimation(xTracker, yTracker, wTracker, hTracker))”中传递的值始终是相同的错误值。无论如何可以解释为什么会这样吗?

        Timeline timeline2 = new Timeline();
for (int f = 0; f < 17; f++) {
System.out.println("xTracker: "+ xTracker + ", yTracker: " + yTracker + ", wTracker: " + wTracker + ", hTracker: " + hTracker);

timeline2.getKeyFrames().add(
new KeyFrame(Duration.millis(durationTracker),
(evt -> swordAnimation(xTracker, yTracker, wTracker, hTracker)))
);
durationTracker += durationPerFrame;
xTracker += xChangePerFrame;
yTracker += yChangePerFrame;
wTracker += wChangePerFrame;
hTracker += hChangePerFrame;
}

timeline2.setCycleCount(1);
timeline2.play();

swordAnimation 方法

    private void swordAnimation(double x, double y, double w, double h) {
System.out.println("x: "+ x + ", y: " + y + ", w: " + w + ", h: " + h);
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.drawImage(swordAnimation[0], x, y, w, h);
}

这里是前 5 次循环的 println 输出,以及调用该方法时的 println 输出。

durationTracker: 0.0
xTracker: -75.0, yTracker: -80.95952023988006, wTracker: 1497.6000000000001, hTracker: 864.0
durationTracker: 16.666666666666668
xTracker: -67.1875, yTracker: -72.52623688155923, wTracker: 1427.4, hTracker: 823.5
durationTracker: 33.333333333333336
xTracker: -59.375, yTracker: -64.09295352323839, wTracker: 1357.2, hTracker: 783.0
durationTracker: 50.0
xTracker: -51.5625, yTracker: -55.65967016491756, wTracker: 1287.0, hTracker: 742.5
durationTracker: 66.66666666666667
xTracker: -43.75, yTracker: -47.22638680659672, wTracker: 1216.8, hTracker: 702.0


x: 57.8125, y: 62.40629685157417, w: 304.19999999999953, h: 175.5
x: 57.8125, y: 62.40629685157417, w: 304.19999999999953, h: 175.5
x: 57.8125, y: 62.40629685157417, w: 304.19999999999953, h: 175.5
x: 57.8125, y: 62.40629685157417, w: 304.19999999999953, h: 175.5
x: 57.8125, y: 62.40629685157417, w: 304.19999999999953, h: 175.5

以及 f=16 时 println 的输出

durationTracker: 266.66666666666663
xTracker: 50.0, yTracker: 53.97301349325333, wTracker: 374.3999999999995, hTracker: 216.0

如果需要更多信息,请告诉我,但我相信这就是所需的全部信息。

最佳答案

您将值存储在字段中。在调用 lambda 表达式时评估这些字段值。这是在循环完成之后,因此每一帧都会看到最终值。

创建 final 局部变量存储中间值并在 lambda 中使用它们:

final double xTrackerLocal = xTracker; // you may need to us a different type here
final double yTrackerLocal = yTracker;
final double wTrackerLocal = wTracker;
final double hTrackerLocal = hTracker;
timeline2.getKeyFrames().add(
new KeyFrame(Duration.millis(durationTracker),
(evt -> swordAnimation(xTrackerLocal, yTrackerLocal, wTrackerLocal, hTrackerLocal)))
);
durationTracker += durationPerFrame;
xTracker += xChangePerFrame;
yTracker += yChangePerFrame;
wTracker += wChangePerFrame;
hTracker += hChangePerFrame;

关于JavaFX Timeline/KeyFrame - 传递的值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58585689/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com