无法绘制多个圆并在x轴上移动它们



我在处理数组时遇到了麻烦。当我运行代码时,即使数组的值为3,也只绘制了一个圆。但是圆圈也不会从右向左移动。当我不使用数组时,代码工作,所以我不确定我是否把错误的东西放在错误的地方,或者如果我错过了什么。我很感激任何人的帮助。

int[] nums = new int [3];
float circleX;
float circleY;
void setup(){
size(500,500);
for(int i = 0; i< nums.length; i++){
nums[i] = int(random(0,400));  
circleX = 460;
circleY = random(40,460);
}
}
void draw(){
background(0);
stroke(255);
strokeWeight(4);
noFill();
for(int i = 0; i < nums.length;i++){
ellipse(circleX,circleY,80,80); 
circleX = circleX - 1;
noLoop();
}
}

下面是一个可能的解决方案。如果你想要三个随机的圆,那么创建x,y坐标的数组。

int _num = 3;
float[] circleX = new float[_num];
float[] circleY = new float[_num];
void setup(){
size(500,500);
for(int i = 0; i< _num; i++){ 
circleX[i] = random(0,400);
circleY[i] = random(40,460);
}
noLoop();
}
void draw(){
background(0);
stroke(255);
strokeWeight(4);
noFill();
for(int i = 0; i <_num;i++){
ellipse(circleX[i],circleY[i],80,80); 
circleX[i] = circleX[i] - 1;   
}
}

最新更新