Draw loop executes 7-8 times after mousepressed before waiting again!

edited August 2015 in Questions about Code

I get 7-8 results printed for every iteration of the mouse being pressed, why this lag in testdone being set or recognized as set to 0 by draw()?

int testdone; // flag to chk if draw loop completed
int row,col,loc;
float sum,avg,c;
PImage img;
void setup() 
{

  size(1000,600);
  testdone=1;
  img = loadImage("c:/processing/epsproc.jpg");
  image(img, 0, 0);

}

 void draw()
   {
   image(img,0,0); 
   if(testdone==0)  //only do once when testdone becomes 1
     {  
     loadPixels();
     sum=0;
     avg=0;
     for (row=100;row<200;row++)
      {
      for(col=100;col<200;col++)
       {
        loc=row*width+col;

        c = green(pixels[loc]);   

        sum=sum+c;

       //println(sum);
       }
      } 
     updatePixels();
     float avg=sum/(100*100);
     println(row,col,sum,avg);
     testdone=1;  // set flag so loop does not get done again till testdone =0
     }

   if(testdone==1)  //wait until mouse pressed to execute draw loop
    {
    if(mousePressed)
     {
     testdone=0;
     }
    }

   }  

Answers

Sign In or Register to comment.