Need a infinite loop

edited September 2015 in Questions about Code

Hi there,

I have the following code which creates the illusion of a moving, checkered floor. I am having trouble creating an infinite loop. Can someone help I would really appreciate it?

boolean boolUseCheckeredFloor = true; 
float offSetZ = 0;

// program: Main parts 

 void setup() {
  size(890, 660, OPENGL);
}

 void draw() {
  background(0);
  Floor();
  offSetZ++;
}

// tab Floor (and Walls)
// Floor: CheckeredFloor 
void Floor () {
  // two ways : 
  if (boolUseCheckeredFloor) {
    CheckeredFloor() ;
    // TheWallsCheckeredFloor ();
  }
  else
  {
      //    FullFloor();
    //    TheWallsFullFloor ();
  }
}

void CheckeredFloor() {
 noStroke();
   for (int i = 0; i < 2; i = i+1) {
    for (int j = 0; j < 20; j = j+1) {
      // % is modulo, meaning rest of division 
      if (i%2 == 0) { 
        if (j%2 == 0) { 
          fill (255, 0, 0);
        }
        else
        {
          fill ( 103 );
        }
      }  
      else {
        if (j%2 == 0) { 
          fill ( 103 );
        }
        else
        {
          fill (255, 0, 0);
        }
      } // if

      pushMatrix();
      translate ( 80*i+420, 760, 80*j-1110 + offSetZ );
      box ( 80, 7, 80);  // one cell / tile 
      popMatrix();
    } // for
  } // for
} // function 

Answers

  • The draw() method itself is an infinite loop. If you want some more help, please format your code.

  • Thanks for that Ater. I know the draw() itself is an infinite loop but I cannot get the sketch to run continuously. That is my problem.

  • change line 13 to

    offSetZ = (offSetZ + 1) % 160;  // 160 = two tiles
    

    also, rather than checking i and j are each even, you can check that (i + j) is even, so lines 36-53 can be replaced by

    if ((i + j ) % 2 == 0) { 
        fill(255, 0, 0);
    } else {
        fill(103);
    }
    
  • Hi Koogs,

    Just wanted to say thank you as well for that. It did help.

  • what the new line 13 does is to reset the camera to the beginning when it gets two tiles 'into' the picture. the other alternative is to keep advancing the camera but to add two tiles onto the end of the current tiles (and you can remove the tiles behind you as well - you can't see them any more)

    so it's a bit of a cheat but it might be acceptable - it all depends what you want and what else is going on.

Sign In or Register to comment.