处理停止动画

时间:2018-10-31 21:48:18

标签: java processing

我有以下代码(在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);
  }
}

1 个答案:

答案 0 :(得分:1)

您可以通过将粒子的xy的值与屏幕尺寸进行比较来检查粒子是否超出屏幕范围。例如:

if(x < 0){
  // particle is off left edge of screen
}
else if(x > width){
  // particle is off right edge of screen
}

检测到以下情况之一时,您可以执行以下操作:

  • 从数组中删除粒子,使其在离开屏幕后停止使用系统资源
  • 将值环绕在屏幕的另一侧
  • 通过反转其速度使其跳出边缘

您采用哪种方法完全取决于您要发生的事情。

无耻的自我促进:here是有关处理中冲突检测的教程,其中包括上述方法。