java如何在JavaFX中创建sprite运行周期?
所以,我正在尝试使用JavaFX创建一个游戏。我知道大部分的基础知识,但我不知道如何在JavaFX中创建一个sprite运行周期
我希望能够使用一个方法调用运行周期,比如:
public static void runCycle(){
// execute run cycle, I think the Animation class may help here?
// move image as well, I got that nailed down already though.
}
我知道这不是MRE,但我正试图集思广益,所以如果你有任何建议,请让我知道!:)
链接的图片是精灵表,如果有人能帮我解决这个问题,那就太好了。谢谢
# 1 楼答案
您需要定期更新UI以实现这一点。具体如何实现这一点取决于对其余更新的编码方式。如果你使用
AnimationTimer
创建一个游戏循环,这可能是一个进行这些更新的好地方,但是对于单个图像来说Timeline
似乎是最方便的如何更新GUI取决于绘制图像的方式
Canvas
需要与ImageView
不同的处理。前者要求使用drawImage
方法,允许指定要绘制的源图像部分,后者要求更新viewport
属性以下示例显示了如何使用
ImageView
和Timeline
实现此目的:(不确定这是否是动画中所需的精灵顺序。)