mouseevent - Mouse press event called twice -


the following code not working properly, want happen call placing_shapes_in_screen function mouse press , wait press place object on screen, happening first key press registering twice!! please help?

boolean cont = false;  void setup(){     size(500,400);     background(0,0,53); }    void draw(){     if(mousepressed){     placing_shapes_in_screen();     println("done");     } }   void placing_shapes_in_screen(){      fill(204,0,102);     text("hello", 60, 90);     nofill();        while(cont == false){         if(mousepressed){     ellipse(mousex,mousey,20,20);     cont=true;     }     }     cont=false;  } 

probably issue default draw called 60 time each second. makes relying in var mousepressed control user interaction, not precise, perhaps button still pressed, 1/60th seconds later...

the way go use function mousepressed() or mouseclicked() callback functions called once when 1 of events ocour.

some thing this:

void mousepressed(){ place_shapes_in_screen(); }   void place_shapes_in_screen(){ fill(204,0,102); text("hello", 60, 90); nofill();    ellipse(mousex,mousey,20,20);  } 

edit answer op's comment:

than need check if mouse inside button, , flag (or program states) handle placing ellipse.

in kind of pseudo code:

boolean oktoplace = false;    void mousepressed(){     if(mouseisinsidebutton()){         oktoplace = true;         }else if (oktoplace){         place_shapes_in_screen()     }  }  void place_shapes_in_screen(){     fill(204,0,102);      text("hello", 60, 90);     nofill();        ellipse(mousex,mousey,20,20);     oktoplace = false; // <<<<<<<<<!! } 

edit2

here working 1 online @ sketchpad.cchere:

boolean oktoplace = false; pvector button;   void setup(){     size(300,300);     //using z size     button = new pvector(20, height-40, 20);      }   void draw(){      color f = isinsidebutton()? color(255,0,0):color(0,0,255);    fill(f);     rect(button.x, button.y, button.z, button.z);         }     void mousepressed(){     if(isinsidebutton()){         oktoplace = true;         }else if(oktoplace){             ellipse(mousex, mousey, 80,80);             oktoplace = false;             }     }   boolean isinsidebutton(){     return mousex > button.x &&            mousex < button.x+button.z &&            mousey > button.y &&            mousey < button.y + button.z;     } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -