如果按下按钮,如何在一定时间内显示图像 [英] how to have images be shown for a certain amount of time if a buttons pressed

查看:144
本文介绍了如果按下按钮,如何在一定时间内显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, i'm working on a jumping animation and it works fine but i'm trying to get it so an image will be shown for a certain amount of time (time= time spent in the air) if a button is pressed but return to the original image once the sprite reaches the ground. I've researched quite a lot but i'm yet to find a suitable answer. However i'm thinking of changing the imageview if a certain button is pressed but i'm not sure if this would work.

我的代码

   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();

}


推荐答案

一般情况您可以使用 PauseTransition 执行此操作:

In general you can do this with a 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(...)基本上是无操作,因此应该没有性能问题。)这样,我想,看起来像:

It might be easier though to just detect if you are currently jumping in the animation timer, and call setImage(...) with the appropriate image depending on whether you are jumping or not. (Calling setImage(...) with the same image as is currently used is basically a no-op, so there should be no performance issues doing that.) This would, I think, just look like:

@Override
public void handle(long now) {

    // existing code...

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

    // ...
}

这篇关于如果按下按钮,如何在一定时间内显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆