Using fill() on objects with for loop

edited October 2015 in Questions about Code

I'm creating a game in Processing which allows you to color a grid of rectangles as they become highlighted one by one.

I'm trying to color an array of objects using a for loop and the fill() function, but when I use it, it colors not only colors the current rectangle in the array (which would be correct), but also all previous rectangles. I want it to only color the current rectangle.

    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;
        }
      }
    }

Thanks in advance!

Sign In or Register to comment.