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