how to have images be shown for a certain amount of time if a buttons pressed

前端 未结 1 1534
太阳男子
太阳男子 2021-01-29 08:12

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 but

相关标签:
1条回答
  • 2021-01-29 09:01

    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);
        }
    
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题