I'm making a simple game in JavaFX and I want to display animations when characters fighting. When I click 'attack' button I want to display 'fight.gif' animation. When not clicking anything I want another 'waitForAction.gif' to be displayed. Problem is I want this 'fight.gif' to be displayed for a specific time, so the whole animation can be seen before 'waitForAction.gif' will be displayed again.
So my question is:
Are GIF's a good idea? If Yes, how to control them? If not, how to make these animations work?
If you have problem with understanding I can post pictures of what I want to reach.
Use a Timeline
. E.g. to display the gif for 2 seconds, you could do:
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, e -> { /* display fight.gif */}),
new KeyFrame(Duration.seconds(2), e -> { /* hide fight.gif */})
);
timeline.play();
The way this works is that the timeline consists of a series of key frames. Each key frame has a time associated with it (the time point on the timeline) and, in this example, an action to be invoked at that time. So in this case, zero seconds after the timeline is started, fight.gif
is displayed, and 2 seconds after the timeline is started, fight.gif
is hidden. The timeline is started by invoking its play()
method.
In general, any time you want your UI to do something at, or for, some specific time, an animation of some kind may well provide an easy solution.