为什么我的圈子显示为黑色?

时间:2018-05-19 05:12:44

标签: arrays loops processing draw

我正在尝试制作一个程序,在一个整齐的格子中创建最多24个圆圈,从白色开始,然后满足条件(例如锤子到达屏幕底部并重置),改变那些圆圈按从左到右的顺序 - 每次x轴圆圈到达其行的定义边缘时向下移动一行。

我创建了一个数组,它在索引中保存了24种颜色,所有颜色都设置为白色(与隐形圆的背景相同),但是随着条件逐个变化。然而,当我在下面运行我的代码时,我从一开始就得到了所有黑色圆圈的一个不变的格子而没有考虑锤子,并且每第七行的左上角圆圈中都有一个小白点(只有一个在6排试运行。)

我不知道是什么造成了这种情况,并且多次扫描了代码,我无法看到黑色从何处进入。此时,hammerPos if语句不完整,因为我仍在试图弄清楚发生了什么。

这是我的代码:

int hammerPos = 0; int hammerVel = 1; int hammerPending = 0;
int jump = 60;
int pointIntervX = 20;
int pointIntervY = 20;
int pointX = pointIntervX;
int pointY = pointIntervY;
int pointSize = 10;
color white = color(255,255,255);
color black = color(0,0,0);
color red = color(255,0,0);
color[] pointColour = new color[24];

//standalone method (at top outside of draw and setup)
void pointMaker(color colour, int pointX, int pointY){
  fill(colour);
  ellipse(pointX,pointY,pointSize,pointSize);
}

void setup(){
  size(200,200);
  background(255);
  for(int i:pointColour){//setup pointColour array at BEGINNING of setup
  pointColour[i] = white;
  }
}

void draw(){

  // draw hammer;
  fill(0);
  stroke(0);
  strokeWeight(6);
  line(128,hammerPos+10,148,hammerPos-12);
  strokeWeight(4);
  line(125,hammerPos+13,129,hammerPos+11);
  strokeWeight(8);
  line(115,hammerPos+5,130,hammerPos+20);

  hammerPending = hammerPending + hammerVel;
  if (hammerPending > jump){
    hammerPos = hammerPos + hammerPending;
    hammerPending = 0;
    }

  for(int i = 0;i<24;i++){//refreshes 24 circles in white(invisible) constantly in draw until a color is changed
    pointMaker(pointColour[i], pointX, pointY); 
    pointX += pointIntervX;
    if(pointX>pointIntervX*4){
      pointX = pointIntervX;
      if(pointY<pointIntervY*6){
      pointY += pointIntervY;
      }
    }
  }

  if(hammerPos>height){
     hammerPos = 0;
  }
}

1 个答案:

答案 0 :(得分:0)

for中的draw()循环在strokeWeight(8)仍然有效的上下文中执行。你只看到中风。一个简单的解决方法是将行strokeWeight(1);放在pointMaker定义的开头。

当你这样做时,你会看到另一个错误。 for(int i:pointColour)不符合您的想法。您正在迭代数组pointColour的默认元素(而不是索引) - 所有这些都是0。因此,您在该循环中所做的是连续24次将该数组的第一个元素设置为white,而将其他23个等于0,等于black。因此,在修复strokeWeight错误后,您将留下1个白色和23个黑色圆圈的网格。您应该在创建数组时初始化数组,或者使用setup()

for(int i = 0; i < pointColour.length; i++) {循环遍历索引而不是元素

关于编辑:为了阐明索引与元素的含义:pointColour被声明为color[24]。因此for(int i: pointColour)并没有多大意义。使用这种类型的迭代它应该是for(color i: pointColour)。你写的不是语法错误的唯一原因是Processing的颜色数据类型似乎是32位int的别名。 pointColour中的索引总是24个整数0,1,...,23。 pointColour的元素是您碰巧存储在该数组中的24种颜色。在pointColour[2] = color(0,0,255)中,索引为2,但元素为蓝色