Object 对具有for循环的对象使用fill()

Object 对具有for循环的对象使用fill(),object,for-loop,processing,fill,processing.js,Object,For Loop,Processing,Fill,Processing.js,我正在创建一个处理中的游戏(JavaScript),它允许您在矩形网格一个接一个高亮显示时为其着色 我尝试使用for循环和fill()函数为一个对象数组着色,但是当我使用它时,它不仅为数组中的当前矩形着色(这是正确的),而且为所有以前的矩形着色。我希望它只为当前矩形着色 void setup () { size (1200, 700); background (255, 255, 255); smooth(); rect = new Rectangle [count]; //cr

我正在创建一个处理中的游戏(JavaScript),它允许您在矩形网格一个接一个高亮显示时为其着色

我尝试使用for循环和fill()函数为一个对象数组着色,但是当我使用它时,它不仅为数组中的当前矩形着色(这是正确的),而且为所有以前的矩形着色。我希望它只为当前矩形着色

void setup () {
  size (1200, 700);
  background (255, 255, 255);
  smooth();
  rect = new Rectangle [count]; //creates a grid of possible rectangles, based on the dimensions of the screen
  for (int i = 0; i<= (count-1); i++) {
    fill (255, 255, 255);
    ypos = ypos + heigh;
    if (ypos >= 700) {
      ypos = 0;
      xpos = xpos + wid;
    }
    rect[i] = new Rectangle(xpos, ypos, wid, heigh, redPressed, greenPressed, yellowPressed, bluePressed, blackPressed);
  }
  int now = millis();
}

void draw() {
  for (int i = 0; i < control; i++) {
    if (keyPressed) { //detects if key is pressed and colors the current rectangle that way
      if (key == 'r' ||key == 'R') {
        fill (255, 0, 0);
      }
      if (key == 'g' ||key == 'G') {
        fill (0, 255, 0);
      }
      if (key == 'y' || key == 'Y') {
        fill (255, 255, 0);
      }
      if (key == 'b' || key == 'B') {
        fill (0, 0, 255);
      }
      if (key == 'k' || key == 'K') {
        fill (0, 0, 0);
      }
    }
    stroke (0, 0, 0);
    rect (rect[i].xpos, rect[i].ypos, rect[i].xdim, rect[i].ydim); //draws the current rectangle, moving through the grid
  }
  if (millis() - now >= wait) { //causes a 1 second delay between rectangles
    now = millis();
    control++;
    if (control > (count-1)) {
      control = (count-1);
      i = 0;
    }
  }
}
void设置(){
规模(1200、700);
背景(255、255、255);
光滑的();
rect=new Rectangle[count];//根据屏幕尺寸创建可能的矩形网格
对于(int i=0;i=700){
ypos=0;
xpos=xpos+wid;
}
rect[i]=新矩形(xpos、YPO、wid、heigh、红压、绿压、黄压、蓝压、黑压);
}
int now=millis();
}
作废提款(){
for(int i=0;i=wait){//会在矩形之间造成1秒的延迟
现在=毫秒();
控件++;
如果(控制>(计数-1)){
控制=(计数-1);
i=0;
}
}
}

提前谢谢

首先,我们无法运行此代码,因为您没有发布您的
矩形
类或
控件
,“count”,“xpos”,“ypos”,“wid,
重按
绿色压下
黄色压下
蓝色压下
,或
黑色压下
变量。变量。如果您需要帮助,您必须发布一个我们可以实际运行的日志

第二,你在这里做了一些奇怪的事情:如果你一次只想画一个矩形,为什么要循环遍历你的
矩形
数组?为什么手动导致等待,而不只是设置帧速率

不要每次调用
draw()
时都在矩形中循环,而是跟踪索引并直接访问它。不要自己等待,只需设置帧速率:

int index = 0;
Rectange[] rect;

void setup () {
  size (1200, 700);
  background (255, 255, 255);
  smooth();
  rect = new Rectangle [count]; //creates a grid of possible rectangles, based on the dimensions of the screen
  for (int i = 0; i<= (count-1); i++) {
    fill (255, 255, 255);
    ypos = ypos + heigh;
    if (ypos >= 700) {
      ypos = 0;
      xpos = xpos + wid;
    }
    rect[i] = new Rectangle(xpos, ypos, wid, heigh, redPressed, greenPressed, yellowPressed, bluePressed, blackPressed);
  }
  int now = millis();
  frameRate(1);
}

void draw() {

  if (keyPressed) { //detects if key is pressed and colors the current rectangle that way
    if (key == 'r' ||key == 'R') {
      fill (255, 0, 0);
    }
    if (key == 'g' ||key == 'G') {
      fill (0, 255, 0);
    }
    if (key == 'y' || key == 'Y') {
      fill (255, 255, 0);
    }
    if (key == 'b' || key == 'B') {
      fill (0, 0, 255);
    }
    if (key == 'k' || key == 'K') {
      fill (0, 0, 0);
    }
  }
  stroke (0, 0, 0);
  rect (rect[index].xpos, rect[index].ypos, rect[index].xdim, rect[index].ydim);  
  index++;
  if (index >= rect.length) {
    noLoop();
  }
}
int索引=0;
Rectange[]rect;
无效设置(){
规模(1200、700);
背景(255、255、255);
光滑的();
rect=new Rectangle[count];//根据屏幕尺寸创建可能的矩形网格
对于(int i=0;i=700){
ypos=0;
xpos=xpos+wid;
}
rect[i]=新矩形(xpos、YPO、wid、heigh、红压、绿压、黄压、蓝压、黑压);
}
int now=millis();
帧率(1);
}
作废提款(){
if(keyPressed){//检测是否按下该键,并以这种方式对当前矩形进行着色
如果(键=='r'| |键=='r'){
填充(255,0,0);
}
如果(键=='g'| |键=='g'){
填充(0,255,0);
}
如果(键=='y'| |键=='y'){
填充(255,255,0);
}
如果(键=='b'| |键=='b'){
填充(0,0,255);
}
如果(键=='k'| |键=='k'){
填充(0,0,0);
}
}
冲程(0,0,0);
rect(rect[index].xpos,rect[index].ypos,rect[index].xdim,rect[index].ydim);
索引++;
如果(索引>=矩形长度){
noLoop();
}
}

下次请尽力发布MCVE。

for循环中的“控制”变量是什么?看起来draw函数可以绘制多个矩形,因为它位于for循环中。另外,如何确定数组中的“当前矩形”?@Bergi控制变量强制每个矩形的绘制之间有1秒的延迟。它每1秒增加1。“当前矩形”是指for循环中的任何值。因此,当i=1时,当前三角形为rect[1]。您的意思是ping@AmirS@AmirS控制变量强制每个矩形的绘制之间有1秒的延迟。它每1秒增加1。“当前矩形”是指for循环中的任何值。所以当i=1时,当前三角形是rect[1]。