lundi 26 octobre 2015

NoLoop() Stops MousePressed

I am trying to create a random number of grape objects on a plate. My for loop creates the grapes, however; I realized that if I don't add the code line noLoop(); to stop the random function, then the last grape created will only show.

My issue is that now noLoop() stops my mousePressed actions to happen. I know that noLoop() stops the draw() function which ultimately allows mousePressed to work whenever the user clicks the mouse.

So my question is, is there another way to stop my random from resetting so that all grapes will be created without the noLoop() statement?

int grapeCount = 10;
int grapePosX = 100;
int grapePosY = 300;
//int numberOfGrapes = 5;
boolean newState = false;
PFont font;
boolean stopCreate = true;

void setup(){
 size(800,500);
 font = createFont("Frutiger",20, true);
 textFont(font);
}
void draw(){

 background(255);
 drawTable();
 drawPlate();
 drawGrapes();


 if (mousePressed){   
    background(255);
    //newPlate();
    //newTable();
    //runawayGrape();
    fill(255,0,0);
    text("YOU CANT GET ME!!", 150,100);

  }
}

void drawGrapes(){

//determine how many grapes will be created on plate (5 to 10) and create the grape
for(int i=0; i < numberOfGrapes(); i++){    
  updateGrapePosition();   
}
  //stopCreate = false;
  noLoop();
}
//draw plate background
void drawPlate(){
  fill(255);
  strokeWeight(2);
  ellipse(300,300,500,300);
  ellipse(300,300,450,250); 
}
//draw table background
void drawTable(){
  strokeWeight(0);
  fill(87,67,6);
  rect(0,100,width,400);
}
//determine the random number of grapes created
int numberOfGrapes(){

 int grapeC = round(random(5,10)); 

  return grapeC; 
}
//update position of each grape
void updateGrapePosition(){
  if (grapePosX >= 300){
   grapePosX = grapePosX -30;
  }
   grapePosX = grapePosX + 30;
   Grapes(); 
}
//create each Grape
void Grapes(){
  strokeWeight(1);
  fill(66,2,70);
  //grape Body
  ellipse(grapePosX, grapePosY, 30,30); 
  strokeWeight(5);
  //grape eyes
  point(grapePosX-5,grapePosY-5);
  point(grapePosX+4, grapePosY-5); 
}

void mousePressed(){

 newState = true; 

}

enter image description here




Aucun commentaire:

Enregistrer un commentaire