簡體   English   中英

當對象 position 在時間軸中移動時,如何獲取它的 x 和 y 值?

[英]How do I get the x and y values of an objects position when it is moving in a timeline?

我正在 JavaFX 中制作一個關鍵射擊游戲,其中有浮動的單詞在屏幕上移動。 一旦用戶鍵入正確的單詞,我想在單詞上發射一個“激光”(一個細長的紅色矩形)。 當用戶鍵入正確的單詞時,我需要單詞的 x 和 y 值,這樣我就可以使矩形移動到那個 position。我 go 如何獲取鍵值?

這是我的時間線代碼:

final Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(wordTime),
        new KeyValue (word.getWordBox().translateXProperty(), rand.nextInt(100, 500))));
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(wordTime),
        new KeyValue (word.getWordBox().translateYProperty(), rand.nextInt(0, 150))));

wordTime 是從用戶輸入中獲取的一個值,它改變了屏幕上單詞的長度。 word.getWordBox() 是在屏幕上移動的單詞。 請讓我知道我是否需要重組這個問題,提供更多細節等。

我試着用

timeline.getKeyFrames().get(1).getValues()

獲取值,但它只是給了我似乎不正確的 KeyValues 列表。 如果這是正確的,請告訴我。

可能最方便獲取的值是單詞在其容器(“父節點”)中的位置。 您可以直接使用

Bounds bounds = word.getWordBox().getBoundsInParent();

然后,例如,做

double x = bounds.getCenterX();
double y = bounds.getCenterY();

等等

由於boundsInParent考慮了應用於節點的任何變換,因此這將包括timeline應用於它的當前轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM