For Loop doesn't seem to do the tasks of its first loop only

edited December 7 in Questions about Code

Hello,

I have the following code, where I generate a given number of instances of two classes in a grid. For some reason, the first instances of each classes are not drawn, even though the loop does go through the first loop in both cases (checked with a println("chief/grid added:", i/j/k);). Tried putting the symbols() and grid() into setup(), but that produced the same result. Also, when the symbols() and grid() functions run without the added != true test (i.e. adding an instance every frame, eventually causing a crash), then the second drawing of the instance does draw the first instances of both classes.

Thanks in advance for the help.

ArrayList<Chief> chiefs;
ArrayList<Climb> climbers;
ArrayList<Grid> grids;
ArrayList<Spider> spiders;
ArrayList<Star> stars;

float globalKill = .05;
float dontKill = 0;

boolean addedChiefs = false;
boolean addedGrids = false;

void setup() {
  fullScreen(P3D);
  smooth(8);
  frameRate(30);
  noCursor();

  blendMode(LIGHTEST);

  chiefs = new ArrayList<Chief>();
  climbers = new ArrayList<Climb>();
  grids = new ArrayList<Grid>();
  spiders = new ArrayList<Spider>();
  stars = new ArrayList<Star>();
}

void draw() {
  background(0);

  symbols(5);
  grid(5);

  showInstances();
}

void symbols(int _rep) {
  if (addedChiefs != true) {
    for (int i = 0; i < _rep; i++) {
      for (int j = 0; j < _rep; j++) {
        chiefs.add(new Chief(width/_rep/2 + width/_rep*i, height/_rep/2+ height/_rep*j, 0, height/_rep/(_rep*3), 3, dontKill));
        println("chief added: ", i, j);
      }
    }
    addedChiefs = true;
  }
}

void grid(int _rep) {
  if (addedGrids != true) {
    for (int k = 1; k <= _rep-1; k++) {
      grids.add(new Grid(width/_rep*k, 0, 0, _rep, 50/_rep, 1, dontKill)); // vertical
      grids.add(new Grid(0, height/_rep*k, 0, _rep, 50/_rep, 2, dontKill)); // horizontal
      println("grid added: ", k);
    }
    addedGrids = true;
  }
}

void showInstances() {
  for (int i = chiefs.size()-1; i >= 1; i--) {
    Chief chief = chiefs.get(i);
    chief.show();
  }

  for (int i = grids.size()-1; i >= 1; i--) {
    Grid grid = grids.get(i);
    grid.show();
  }
}

Edit: below is the log of the println()'s used in the code above.

chief added:  0 0
chief added:  0 1
chief added:  0 2
chief added:  0 3
chief added:  0 4
chief added:  1 0
chief added:  1 1
chief added:  1 2
chief added:  1 3
chief added:  1 4
chief added:  2 0
chief added:  2 1
chief added:  2 2
chief added:  2 3
chief added:  2 4
chief added:  3 0
chief added:  3 1
chief added:  3 2
chief added:  3 3
chief added:  3 4
chief added:  4 0
chief added:  4 1
chief added:  4 2
chief added:  4 3
chief added:  4 4
grid added:  1
grid added:  2
grid added:  3
grid added:  4

Edit 2: What the resulting canvas looks like.

Answers

  • Answer ✓

    Solved: in showInstances(), the for loops need to decrease to 0, not 1.

  • Answer ✓
    for (int i = chiefs.size()-1; i >= 1; i--) {
      Chief chief = chiefs.get(i);
      chief.show();
    }
    

    is there a reason they are being drawn backwards?

    if not, you could replace that with

    for (Chief c : chiefs) {
      c.show();
    }
    
Sign In or Register to comment.