【问题标题】:How to use random over time in Processing如何在处理中随时间使用随机
【发布时间】:2021-06-21 15:58:08
【问题描述】:

我正在尝试使用 random(); 更改一些圆圈对象的位置。我的问题是我要么可以在 setup() 中初始化随机位置……(不能在 draw() 中覆盖它们……或者在 draw() 中初始化它们……所以一切都发生得太快了……我不想放慢速度down frameRate() 或者...所以我认为编写一个额外的函数并且每 30 帧调用一次就可以解决它 - 但是我的圆圈对象被当作一个对象处理并绘制在同一个位置...我错过了什么?我认为逻辑有点棘手?感谢您的任何帮助!

Circle[] circles = new Circle[3];    
float rX;
float rY;    
float posX;

void setup () {
  size(540, 960);
  randomizePositions();
}
   
void randomizePositions() {
  rX = random(width);
  rY = random(height);
}
    
void draw() {
  background(#c1c1c1);
  for (int i = 0; i <circles.length; i++) { 
    circles[i] = new Circle(rX, rY);
  }
    
  for (int i = 0; i < circles.length; i++) {
    circles[i].display();
  }

  if (frameCount % 30 == 0) {
    randomizePositions();
  }
}

这是我的对象:

class Circle {

  int size;
  float x;
  float y;
 
  Circle(float tempX, float tempY) {
    size = width/10;
    x = tempX;
    y = tempY;
  }

  void display() {
    float wave = sin(radians(frameCount*10));
    for (int i = 0; i < 10 * wave; i++) {
      noFill();
      stroke(0);
      strokeWeight(20);
      ellipse(x, y, i * size, i * size);
    }
  }
}

【问题讨论】:

    标签: arrays object random processing


    【解决方案1】:

    我想我找到了解决办法!这就是我要解决的方法——把它全部放在自定义函数中:

      void setup () {
      size(540, 960);
      randomizePositions();
    }
    
    
    void randomizePositions() {
      for (int i = 0; i <circles.length; i++) {
        rX = random(width);
        rY = random(height);
        circles[i] = new Circle(rX, rY);
      }
    }
    
    void draw() {
      background(#c1c1c1);
      for (int i = 0; i < circles.length; i++) {
        circles[i].display();
      }
      if (frameCount % 30 == 0) {
        randomizePositions();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多