how to have images be shown for a certain amount o

2020-05-09 19:26发布

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.

my code

   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条回答
贪生不怕死
2楼-- · 2020-05-09 19:39

In general you can do this with a PauseTransition:

Duration delay = ... ;
PauseTransition timer = new PauseTransition(delay);
timer.setOnFinished(evt -> undoWhatIDidToUI());
doSomethingToUI();
timer.play();

So, if I understand you,

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

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

    // ...
}
查看更多
登录 后发表回答