无法摆脱 Processing sketch 中的视觉伪影

Unable to get rid of visual artifacts in Processing sketch

int numFrames = 5; //Number of animation frames
int frame = 0;
PImage[] images = new PImage[numFrames]; //Image array

void setup() 
{
    size(800, 800);
    background(180, 180, 180);
    frameRate(15); //Maximum 30 frames per second 
}

void draw() 
{
    images[0] = loadImage("Ayylmfao.0001.png");
    images[1] = loadImage("Ayylmfao.0002.png");
    images[2] = loadImage("Ayylmfao.0003.png");
    images[3] = loadImage("Ayylmfao.0004.png");
    images[4] = loadImage("Ayylmfao.0005.png");
    frame++;
        if (frame == numFrames) 
        {
            frame = 0;
        }

    image(images[frame], 0, 0);
}

所以我的问题是:当我尝试 运行 这个动画时,我总是从以前的帧中得到一个伪影。我正在使用一个数组来存储动画中的图像,因为我正在尝试练习使用数组。

动画是一个眨眼的眼球。问题是,当它闪烁时,所有以前的帧都被绘制了。眼球的虹膜消失,眼球开始收集前几帧的伪像。

正如 Kevin 指出的那样,您不应该在 draw() 中每秒多次一次又一次地加载图像。您应该在 setup() 中加载一次,然后在 draw():

中渲染它们
int numFrames = 5; //Number of animation frames
int frame = 0;
PImage[] images = new PImage[numFrames]; //Image array

void setup() 
{
    size(800, 800);
    background(180, 180, 180);
    frameRate(15); //Maximum 30 frames per second 
    images[0] = loadImage("Ayylmfao.0001.png");
    images[1] = loadImage("Ayylmfao.0002.png");
    images[2] = loadImage("Ayylmfao.0003.png");
    images[3] = loadImage("Ayylmfao.0004.png");
    images[4] = loadImage("Ayylmfao.0005.png");
}

void draw() 
{

    frame++;
        if (frame == numFrames) 
        {
            frame = 0;
        }

    image(images[frame], 0, 0);
}