I have two timeline which runs inside one function, they are both affect one object, problem is when I play first timeline, other timeline also starts within next line of code, how can I wait until fist timeline finish then play another timeline?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Timeline.play()
is asynchronous—it starts the animation in the background and returns immediately. There are several ways to achieve sequential execution. Which fits best depends on your use case.
- Use just one
Timeline
and move theKeyFrame
s from your second timeline to the first one, with adjusted start duration. - Start the second timeline when the first one is finished:
timeline1.setOnFinished(e -> timeline2.play())
. - If you can, use
Transition
s instead ofTimeline
s and wrap them in aSequentialTransition
.