javascript - Resetting KeyPressed() in Processing -


i have couple of functions in draw method right activated key presses. basically, there's complex grid of squares , each square's color filled in based upon key user presses ('a' makes red, 'b' makes green, etc), method assigned each square.

however, each time first key pressed, fill in of squares based upon key (for example, if first key user presses 'a', squares red instead of first one). how make first key pressed applies first method, second key applies second method, , on, when it's in looping draw() method. here's overly-simplified version below:

void draw(){      boxone();      boxtwo(); } void boxone(){      if(keypressed){        if(key == 'a'){          fill(red);        }        if(key == 'b'){          fill(green);        }        rect(10, 10, 10, 10);      } } void boxtwo(){      if(keypressed){        if(key == 'a'){          fill(red);        }        if(key == 'b'){          fill(green);        }        rect(20, 20, 10, 10);      } } 

i guess, there anyway "reset" keypressed option takes in key next time?

i think want function keypressed() not boolean global variable keypressed. see docs @ http://processing.org/reference/keypressed_.html.

in keypressed() can map key particular box - show simple non-optimal way here key maps index lets params correct rectangle show. that's simple not general or pretty works.

i'd suggest avoiding repetitive code have in boxone() , boxtwo() - differ in box coordinates. if replaced more general fillbox() driven box number.

if want 1 box showing @ time, call background() function before calling fillbox().

int box = -1; int boxparams[] = {10, 10, 10, 10                  , 20, 20, 10, 10                  , 30, 30, 10, 10};  void draw(){     //background(128, 128, 128);       if(box >= 0) {         fillbox(box);     } }  void keypressed() {    if(key == 'a')        box = 0;    else if(key == 'b')        box = 1;    else if(key == 'c')        box = 2;    else        box = -1; }  void fillbox(int box){    if(key == 'a'){      fill(255, 0, 0);    }    else if(key == 'b'){      fill(0,255, 0);    }    else if(key == 'c'){      fill(0,0, 255);    }    rect(boxparams[box * 4], boxparams[box * 4 + 1], boxparams[box * 4 + 2], boxparams[box * 4 + 3]); } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -