如果按下按钮,如何在一定时间内显示图像

时间:2018-01-10 21:19:25

标签: java animation javafx

所以,我正在制作一个跳跃动画并且它工作正常,但是我试图得到它,这样一个图像将显示一段时间(时间=在空中花费的时间),如果一个按钮是一旦精灵到达地面,按下但返回原始图像。我已经研究了很多,但我还没找到合适的答案。但是,如果按下某个按钮,我正在考虑更改imageview,但我不确定这是否有效。

我的代码

   public void start(Stage primaryStage) throws Exception {
    final Group root = new Group();
    Scene scene = new Scene(root, 640, 480, Color.ALICEBLUE);

    Image imgninja = new Image(getClass().getResourceAsStream("ninja_sprite.png"));
    Image tempground = new Image(getClass().getResourceAsStream("ground.png"));
    Image tempground2 = new Image(getClass().getResourceAsStream("ground.png"));
    final Image test = new Image(getClass().getResourceAsStream("myninja_1.png"));
    final Rectangle r = new Rectangle();
    r.setX(rectx);
    r.setY(recty);
    r.setWidth(50);
    r.setHeight(100);
    r.setArcWidth(20);
    r.setArcHeight(20);

    Button button = new Button();
    iview = new ImageView(imgninja);
    iview.setLayoutX(ninjax);
    iview.setLayoutY(ninjay);
    iview2 = new ImageView(tempground);
    iview2.setLayoutX(backgroundx);
    iview2.setLayoutY(backgroundy);
    iview3 = new ImageView(tempground2);
    iview3.setLayoutX(555);
    iview3.setLayoutY(backgroundy);
    iview4 = new ImageView(test);
    iview4.setLayoutX(aix);
    iview4.setLayoutY(aiy);


    root.getChildren().addAll(iview2, iview3, button, iview, r,iview4);
    primaryStage.setTitle("A basic window");
    primaryStage.setScene(scene);
    primaryStage.show();

    button.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {

            if (event.getCode() == KeyCode.LEFT) {

                ninjax = ninjax - 10;
                iview.setLayoutX(ninjax);
            }

            if (event.getCode() == KeyCode.UP) {

                if (ninjay > 365) {
                    jumpforce = -16;

                }
                iview.setLayoutY(ninjay);

            }
            if (event.getCode() == KeyCode.RIGHT) {


                ninjax = ninjax + 10;

                iview.setLayoutY(ninjax);

            }

        }

    });

    new AnimationTimer() {


        @Override

        public void handle(long now) {

            if (iview.getBoundsInParent().intersects(iview2.getBoundsInParent())) {

                groundforce = -gravity;

            } else {
                groundforce = 0;

            }

            if (jumpforce < 0) {

                ninjay = ninjay - (-gravity) + jumpforce + groundforce;
                ninjax = ninjax + 3;

                jumpforce = jumpforce + 1;

            } else if (ninjay < 365) {
                ninjay = ninjay - (gravity) + jumpforce + groundforce;

                ninjax = ninjax + 3;

            }

            iview.setLayoutY(ninjay);
            iview.setLayoutX(ninjax);

        }

    }.start();

}

1 个答案:

答案 0 :(得分:0)

通常,您可以使用PauseTransition

执行此操作
Duration delay = ... ;
PauseTransition timer = new PauseTransition(delay);
timer.setOnFinished(evt -> undoWhatIDidToUI());
doSomethingToUI();
timer.play();

所以,如果我了解你,

Duration delay = ... ; // time for a jump
PauseTransition jumpTimer = new PauseTransition(delay);
jumpTimer.setOnFinished(evt -> iview.setImage(imgninja));
iview.setImage(jumpingImage);
jumpTimer.play();

可能更容易检测您当前是否正在跳过动画计时器,并根据您是否跳跃来使用适当的图像调用setImage(...)。 (使用与当前使用的图像相同的图像调用setImage(...)基本上是无操作,因此应该没有性能问题。)我认为,这看起来像:

@Override
public void handle(long now) {

    // existing code...

    if (jumpforce < 0) {
        iview.setImage(jumpingImage);
    } else {
        iview.setImage(imgninja);
    }

    // ...
}