Mouse press event called twice

2.1k views Asked by At

The following code is not working properly, what i want to happen is to call the placing_shapes_in_screen function with a mouse press and then wait for another press to place an object on the screen, however what is actually happening is that the first key press is 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;

}
2

There are 2 answers

1
v.k. On BEST ANSWER

Probably the issue is that by default draw is called 60 time each second. That makes relying in the var mousePressed to control user interaction, not very precise, perhaps the button was still pressed, 1/60th seconds later...

The way to go is to use the function mousePressed() or mouseClicked() that are callback functions called once when one of those events ocour.

some thing like 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 to answer OP's comment:

Than you need to check if the mouse is inside the button, and a flag (or program states) to handle placing the 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 a working one online at sketchpad.cchere:

boolean okToPlace = false;
PVector button;


void setup(){
    size(300,300);
    //using z for 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;
    }
6
Kevin Workman On

I want the second mouse press to be separate to the first.

Consider using the mousePressed() function instead of the mousePressed variable. Something like this:

void setup(){
 background(0); 
}

void draw() {}

void mousePressed() {
   println("Mouse pressed.");
   ellipse(mouseX, mouseY, 10, 10);
}

The mousePressed variable is simply true whenever the mouse is held down, so you don't get separate events for different clicks. The mousePressed() function is only called once per mouse click though, so you can separate different clicks into different events.

Note that there are other event functions such as mouseReleased() and mouseClicked(). You can check them out in the Processing reference.