我如何通过处理Java速度放慢精灵动画
我正在尝试放慢我从此网站上找到的精灵动画: https:https:// processing。 org/示例/AnimatedSprite.html
这是动画的类:
class Animation {
PImage[] images;
int imageCount;
int frame;
Animation(String imagePrefix, int count) {
imageCount = count;
images = new PImage[imageCount];
for (int i = 0; i < imageCount; i++) {
// Use nf() to number format 'i' into four digits
String filename = "images/" + imagePrefix + nf(i, 2) + ".png";
images[i] = loadImage(filename);
}
}
void display(float xpos, float ypos) {
frame = (frame+1) % imageCount;
image(images[frame], xpos, ypos);
}
int getWidth() {
return images[0].width;
}
}
从显示动画的网站上,它说:
编写一个程序以显示动画gif,但不允许对显示顺序和显示速率进行太多的控制。
到目前为止,动画以当前的帧速率播放(大约60个fps),但是如果我想在5 fps时玩它怎么办?显然,我不能只是放慢整个框架,因为我只想动画放慢脚步,而不是整个项目/画布。
我尝试通过执行这样的操作来减慢动画的速度:
if (frameCount % 10 == 1) {
//Show image
}
但是上面的方法闪烁了图像,只显示每十个帧。 我将如何成功降低动画的显示速度,以使框架看起来不会这么快?
这是动画现在的样子:
i.sstatic.net/ccuoc.gif“ rel =“ nofollow noreferrer 这就是我想要的外观:
感谢您的任何帮助。
I'm attempting to slow down a sprite animation that I found from this website here: https://processing.org/examples/animatedsprite.html
Here is the class for the animation:
class Animation {
PImage[] images;
int imageCount;
int frame;
Animation(String imagePrefix, int count) {
imageCount = count;
images = new PImage[imageCount];
for (int i = 0; i < imageCount; i++) {
// Use nf() to number format 'i' into four digits
String filename = "images/" + imagePrefix + nf(i, 2) + ".png";
images[i] = loadImage(filename);
}
}
void display(float xpos, float ypos) {
frame = (frame+1) % imageCount;
image(images[frame], xpos, ypos);
}
int getWidth() {
return images[0].width;
}
}
From the website showing the animation, it says:
It would be easy to write a program to display animated GIFs, but would not allow as much control over the display sequence and rate of display.
So far, the animation plays at the current frame rate (around 60 FPS), but what if I want to play it at 5 FPS? Obviously, I can't just slow down the entire framerate, because I want just the animation to slow down, not my entire project/canvas.
I've tried slowing the animation down by doing something like this:
if (frameCount % 10 == 1) {
//Show image
}
But the above approach flashes the image, only showing it every ten frames. How would I successfully slow down the rate of display of the animation, so that the frames don't appear so quickly?
Here's what the animation looks like right now:
And here's what I want it to look like:
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你太接近了! :)
您几乎使用了
framecount
的解决方案,只需要将框架更新与图形分开。您想根据FrameCount(每隔一段时间)进行更新,但要连续显示:(感觉到12(60/5)最适合您的项目)
,您也可以使用
You're sooo close ! :)
You've got the solution pretty much using
frameCount
, just need to separate the frame update from the drawing. You want to update based on the frameCount (every once in a while), but display continuously:(feel to ajust 12 (60/5) to what suits your project best)
Shameless plug, you can also use the image-sequence-player library I wrote which allows you to call
setDelay(1000/5)
to have the image sequence play at 5fps.