问题描述
我正在尝试使用 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);
}
}
}
解决方法
我想我找到了解决方案!这就是我将如何解决它 - 将其全部放入自定义函数中:
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();
}
}