mousePressed - not registering just once

Hi, I’m very new to processing,

I’m trying to write a piece of code which plays and pauses a video, and want it so that when i press a certain area of the GUI it pauses the video (and displays a pause symbol) and when pressed again it plays it… However I cannot get this to work. It seems to only like to do one or the other, (depending on the placement of code), I think what it is doing is registering once - executing the command, but then registering another click, hence pausing the video. I hope this makes sense (i’m not sure i really understand myself!)

Here is the code:

void mousePressed(){
  
 if ((pause == true)&& (mousePressed == true)  && (mouseX > 540) && (mouseX < 600) && (mouseY > 0) && (mouseY < 45)){
 pause = false;

 println(pause + "1");
} 
 if ((pause == false)&& (mousePressed == true)  && (mouseX > 540) && (mouseX < 600) && (mouseY > 0) && (mouseY < 45)){
pause = true;

 println(pause + "2");
 }
}

(the println() is there for troubleshooting)

Thanks so much for any help or light you can spread on this for me :slight_smile:

Ben

1 Like

You don’t need this; your code is already running one time per mouse press because it is in mousePressed().

You should either have an if-else structure , or you could return.

void mousePressed(){
  if( on_button && paused ){
    paused = false;
  } else if ( on_button && !paused ){
    paused = true;
  }
}

or

void mousePressed(){
  if( on_button && paused ){
    paused = false;
    return;
  }
  if ( on_button && !paused ){
    paused = true;
    return;
  }
}

Or even just:

void mousePressed(){
  if( on_button ){
    paused = !paused;
  }
}
3 Likes

Amazing!! Thank you so much

1 Like