小编典典

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

java

因此,我正在制作一个跳跃动画,并且效果很好,但是我正在尝试获取它,因此如果按下按钮但返回一定时间后,图像将显示一定的时间(时间=空中停留的时间)精灵到达地面后,恢复到原始图像。我已经研究了很多,但是还没有找到合适的答案。但是我正在考虑如果按下某个按钮来更改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();

}

阅读 227

收藏
2020-12-03

共1个答案

小编典典

通常,您可以使用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);
    }

    // ...
}
2020-12-03