我制作了一个动画,其中许多线条(水滴)掉落;通过用鼠标左键单击,您只需降低它们的速度即可。我还想做的就是在跌落时控制其Y值:当我用鼠标右键单击时,他们都会跟随它。

Drop[] drops = new Drop[270]; // array

void setup() {
     size(640, 360); // size of the window
     for (int i = 0; i < drops.length; i++) {
         drops[i] = new Drop();
    }
}

void draw() {

    background(52);

    for (int i = 0; i < drops.length; i++) {
        drops[i].fall();
        drops[i].show();
        drops[i].noGravity();
    }
}


和Drop类:

class Drop {
    float x = random(width); // posizione x di partenza
    float y = random(-180,-100); // posizione y di partenza
    float yspeed = random(2,7); // velocità random

    void fall() {
        y += yspeed;

        if (y > height) { // riposizionamento delle gocce
            y = random(-180,-100);
        }
    }

    void noGravity(){ //
        if(mousePressed && (mouseButton == LEFT)){
            y -= yspeed*0.75;
        }

        if(mousePressed && (mouseButton == RIGHT)){
              this.y = mouseY + yspeed;
        }
    }

    void show() { // funzione per l'aspetto delle gocce
        stroke(52, 82, 235);
        line(x,y,x,y+20);
    }
}


我正在谈论的函数是noGravity(),但是当我单击鼠标右键时,跟随我的鼠标,所有滴都排成一行。有什么简单的建议吗?谢谢你们!!!

最佳答案

右键单击时更改y位置与更改液滴移动的速度不同。您可能只是没有注意到。

在这里,尝试更改这些行中在noGravity()中单击鼠标右键的部分:

yspeed = abs(yspeed); //this is so the drops behaves normally again when you stop right clicking
if(mousePressed && (mouseButton == RIGHT)){
  if (mouseY < this.y) { //this makes the drops go toward the mouse position
    yspeed = yspeed * -1; //going up is negative speed
  }
}


这有点酷。请注意,如果按住鼠标右键,当移动鼠标时,液滴将尝试以自己的速度跟随。我不知道你在做什么,但我喜欢。

我不确定期望的结果如何,所以请让我知道是否误解了。

09-26 20:20
查看更多