如何在 voice draw() 中只执行一次函数;但仍会在整个动画中显示

How to execute a function only once in voice draw(); but still have it displayed for the whole animation

我在这里遇到了一些问题。

基本上我做了一个有几个函数的动画。我希望功能具有动画效果,但我希望保持静止的功能除外。

如果我指定 noLoop,则没有任何动画,如果我告诉函数 运行 设定的次数,那么在该次数之后它停止显示。我想要的是 运行 一次,然后仍然显示。

你们知道我该怎么做吗?

代码如下:

int r1,r2,r3 = 0;
int i1, i2,i3;

void setup(){
    size(800,800);
    background(255,0);
    //noLoop();
}

void draw(){
    background(255,0);
    rosace();

    croix();
    sillon1();
    sillon2();
}

void rosace(){
    for (i1 = 0;i1<230; i1++){

        rectMode(CENTER);

        noFill();
        stroke(20);
        strokeWeight(1);


        pushMatrix();
        translate(width/2,height/2);
        translate(400,400);
        rotate(radians(r1));
        rect(0,0,400,400);
        r1 +=1;
        //println(r);

        popMatrix();
        println("rosace ex");
    }
}

void croix(){

    pushMatrix();
    strokeWeight(2);
    stroke(0);
    translate(width/2, height/2);
    rotate(radians(45));
    line(-10,0,10,0);
    line(0,-10,0,10);
    popMatrix();
}

void sillon1(){
    for (i2 =0; i2<360; i2++){
        pushMatrix();

        translate(width/2,height/2);
        strokeWeight(int(random(0,7)));

        rotate(radians(r2));

        point(-330,0);

        popMatrix();
        r2 +=1;
    }
}

void sillon2(){
    for (i2 =0; i2<2000; i2++){
        pushMatrix();

        translate(width/2,height/2);
        strokeWeight(int(random(1,3)));

        rotate(radians(r2));

        point(-360,0+(random(-2,2)));

        popMatrix();
        r2 +=1;
    }
}

void rosace(); 是我想要的非动画。

问题是您将 r1 存储为全局变量。这意味着它在每次迭代后都保持其值。

相反,将其设为局部变量:

void rosace() {
    int r1 = 0;

    for (i1 = 0; i1<230; i1++) {
        rectMode(CENTER);

        noFill();
        stroke(20);
        strokeWeight(1);

        pushMatrix();
        translate(width/2, height/2);
        translate(400, 400);
        rotate(radians(r1));
        rect(0, 0, 400, 400);
        r1 +=1;

        popMatrix();
        println("rosace ex");
    }
}

当在其上绘制动画时,您不能只保留场景的一部分。您必须在每一帧中绘制整个场景。确保 rosace 在每一帧中绘制相同的内容。

只需在每一帧中设置r1 = 0,然后rosace中的算法在每一帧中生成相同的矩形:

void rosace(){
    r1 = 0;

    for (i1 = 0;i1<230; i1++){
        // [...]
    }
}