如何让对象在javafx中以恒定速度下降/移动?

时间:2017-04-11 00:08:48

标签: java javafx shapes

我是编码的初学者,正在尝试使用javafx制作简单的游戏。此刻,我正试图让一个矩形以恒定的速度落在屏幕上。我尝试使用getLayoutY(),它在EventHandler方法中工作,我必须按下箭头,但我希望对象在没有用户输入的情况下下降/移动。我搜索的资源主要涉及精灵动画,这对我来说有点太复杂了。 这是矩形:

    Rectangle rect2 = new Rectangle();                 
    rect2.setX(500);
    rect2.setY(500);
    rect2.setWidth(100);
    rect2.setHeight(100);
    rect2.setFill(Color.BLUE);
    scene.getChildren().addAll(rect2);

此代码仅适用于scene.setOnKeyPressed()...方法

    rect2.setLayoutY(rect2.getLayoutY() + 10);

1 个答案:

答案 0 :(得分:0)

我认为转换是您所需要的:http://docs.oracle.com/javafx/2/animations/basics.htm

我的情况(未经测试):

Rectangle rect2 = new Rectangle();                 
rect2.setX(500);
rect2.setY(500);
rect2.setWidth(100);
rect2.setHeight(100);
rect2.setFill(Color.BLUE);
scene.getChildren().addAll(rect2);
Path path = new Path();
path.getElements().add (new MoveTo (0, 0));
path.getElements().add (new LineTo(0, 100));

PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.millis(10000));
pathTransition.setNode(cbTypeCrc);
pathTransition.setPath(path);
pathTransition.setOrientation(OrientationType.NONE);
pathTransition.setAutoReverse(true);
pathTransition.play();