我有以下代码(在Processing Software中),我想在粒子离开屏幕之前停止它们… 所以我不知道如何将它们停在屏幕边缘附近……
请咨询...
谢谢
Particle [] pickles = new Particle [100];
void setup () {
size (500, 500);
smooth ();
for (int i=0; i<pickles.length; i++) {
pickles [i] = new Particle ();
}
}
void draw () {
background (0); //clear the background
for (int i=0; i<pickles.length; i++) {
pickles[i].update();
}
}
class Particle {
float x;
float y;
float velX ; // speed or velocity
float velY;
Particle () {
//x and y position to be in middle of screen
x = width/2;
y = height/2;
velX = random (-10, 10);
velY = random (-10, 10);
}
void update () {
x+=velX;
y+=velY;
fill (255);
ellipse (x, y, 10, 10);
}
}
答案 0 :(得分:1)
您可以通过将粒子的x
和y
的值与屏幕尺寸进行比较来检查粒子是否超出屏幕范围。例如:
if(x < 0){
// particle is off left edge of screen
}
else if(x > width){
// particle is off right edge of screen
}
检测到以下情况之一时,您可以执行以下操作:
您采用哪种方法完全取决于您要发生的事情。
无耻的自我促进:here是有关处理中冲突检测的教程,其中包括上述方法。