【问题标题】:Unable to draw multiple circles and move them on x axis无法绘制多个圆圈并在 x 轴上移动它们
【发布时间】:2021-10-20 10:47:06
【问题描述】:

我在处理数组时遇到了问题。当我运行我的代码时,即使数组的值为 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();
 }
}

【问题讨论】:

    标签: arrays encoding processing


    【解决方案1】:

    以下一种可能的解决方案。如果您想要三个随机圆,则创建 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;   
     }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多