【发布时间】:2018-10-18 00:12:52
【问题描述】:
我目前正在使用 JavaFX 制作一个打字速度游戏,其中单词应该从顶部落下,并且用户必须在落到底部之前尽可能快地键入它们。我已经准备好游戏的基本设置。我唯一苦苦挣扎的是如何让单词从顶部掉到底部(目前它们是从底部到顶部)。而且我还希望多个单词在它们之间的特定时间间隔(比如 30 毫秒)从顶部的随机位置(不是同一个原点)落下。我到目前为止的代码:
public void showWords() throws InterruptedException
{
int missedWords = 0; // number of words the user failed to type
while (missedWords != 10)
{
dequedWord = queue.dequeue(); // the word that the Text object will contain
Text runWord = new Text(dequedWord);
wordsPane.getChildren().add(runWord); // the canvas in which the words will travel from top to bottom
double PaneHeight = wordsPane.getHeight();
//double PaneWidth = wordsPane.getWidth();
double runWordWidth = runWord.getLayoutBounds().getWidth();
KeyValue initKeyValue = new KeyValue(runWord.translateYProperty(), PaneHeight);
KeyFrame initFrame = new KeyFrame(Duration.ZERO, initKeyValue);
KeyValue endKeyValue = new KeyValue(runWord.translateYProperty(), -1.0 * runWordWidth);
KeyFrame endFrame = new KeyFrame(Duration.seconds(12), endKeyValue);
Timeline timeline = new Timeline(initFrame, endFrame);
timeline.setCycleCount(1);
timeline.play();
// add code to check whether user typed the word in the Text object
missedWords++;
}
}
我是动画新手,所以我对 Timeline、KeyValue 和 KeyFrame 类了解不多。我尝试阅读 API 的文档,但对我没有多大帮助。任何帮助是极大的赞赏。谢谢你:)
【问题讨论】:
标签: java animation javafx javafx-8 game-development