How do I make a grid of objects?

edited July 2015 in How To...

Hi, I'm new to processing and I'm trying to create a grid of rectangles that flash different colors at random intervals.

Here's my code:

int size = 300;
int tileSize = 15;
int tileNumber = size / tileSize;
tile [] tileField = new tile [tileNumber];

void setup() {
  size(size, size);
  for (int i = 0; i < tileNumber; i++) {
    for (int j = 0; j < tileNumber; j++) {
      tileField[i] = new tile(i * tileSize, j * tileSize, 255, 255, 255, 5);
    }
  }
}

void draw() {
  background(0);
  for(int i = 0; i < tileNumber; i++) {
    tileField[i].tileRun();
  }
}

class tile {
 int tileX;
 int tileY;
 int tileRed;
 int tileGreen;
 int tileBlue;
 int tileActive;

 tile(int _tileX, int _tileY, int _tileRed, int _tileGreen, int _tileBlue, int _tileActive) {
   tileX = _tileX;
   tileY = _tileY;
   tileRed = _tileRed;
   tileGreen = _tileGreen;
   tileBlue = _tileBlue;
   tileActive = _tileActive;
 }

 void tileRun() {
   tileDisplay();
   tileActivate();
 }

 void tileDisplay() {
   fill(tileRed, tileGreen, tileBlue);
   rect(tileX, tileY, tileSize, tileSize);
 }

 void tileActivate() {
   tileActive = int(random(501));
   if (tileActive == 0) {
     tileRed = int(random(256));
     tileGreen = int(random(256));
     tileBlue = int(random(256));
   }
   else {
     tileRed = 0;
     tileGreen = 0;
     tileBlue = 0;
   }
 }
}

However when I run the code only the squares on the bottom of the screen flash. Is it a problem with my forLoop?

Answers

  • Answer ✓

    Yes, you are using i which is just the outer loop and gets overwritten in the inside

    instead say int k=0; before the first for loop and

    k++; after defining the object

    use k as an index for the array

  • Answer ✓

    This is in setup()

    draw() is ok

  • I don't understand... I'm afraid I'm really just starting processing...

  • Oh damn, I understood! Thanks alot!

  • edited July 2015

    Actualy I'm so sorry. I realized the code I tried using doesn't do what I want it to do. I think I'm following your instructions incorrectly.

    void setup() {
      size(size, size);
      int k = 0;
      for (int i = 0; i < tileNumber; i++) {
        ++k;
        for (int j = 0; j < tileNumber; j++) {
          tileField[i] = new tile(i * tileSize, k * tileSize, 255, 255, 255, 5);
        }
      }
    

    I tried using this but it only makes the squares that intersect the diagonal from point 0, 0 to point 400, 400...

  • line 4

    tileNumber * tileNumber

    ?

  • I'm sorry I really don't understand. What tileNumber * tileNumber on line 4?

  • tile [] tileField = new tile [tileNumber*tileNumber];

  • also k++;

    after the line

    tileField[k] = ..........

    (And use k here)

  • // grid 
    int size = 300;
    int tileSize = 15;
    int tileNumber = size / tileSize;
    tile [] tileField = new tile [tileNumber*tileNumber];
    
    void setup() {
      size(size, size);
    
      int k=0;
      for (int i = 0; i < tileNumber; i++) {
        for (int j = 0; j < tileNumber; j++) {
          tileField[k] = new tile(i * tileSize, j * tileSize, 255, 255, 255, 5);
          k++;
        }
      }
    }
    
    void draw() {
      background(0);
      for (int i = 0; i < tileField.length; i++) {
        tileField[i].tileRun();
      }
    }
    
    class tile {
      int tileX;
      int tileY;
      int tileRed;
      int tileGreen;
      int tileBlue;
      int tileActive;
    
      tile(int _tileX, int _tileY, int _tileRed, int _tileGreen, int _tileBlue, int _tileActive) {
        tileX = _tileX;
        tileY = _tileY;
        tileRed = _tileRed;
        tileGreen = _tileGreen;
        tileBlue = _tileBlue;
        tileActive = _tileActive;
      }
    
      void tileRun() {
        tileDisplay();
        tileActivate();
      }
    
      void tileDisplay() {
        fill(tileRed, tileGreen, tileBlue);
        rect(tileX, tileY, 
        tileSize, tileSize);
      }
    
      void tileActivate() {
        tileActive = int(random(501));
    
        if (tileActive == 0) {
          tileRed = int(random(256));
          tileGreen = int(random(256));
          tileBlue = int(random(256));
        } else {
          tileRed = 0;
          tileGreen = 0;
          tileBlue = 0;
        }
      }
    }
    //
    
Sign In or Register to comment.