Waiting for mousePressed() in draw

Hello,

I’m very new to programming with Processing, and I just wanted to know, how I can force the program in draw() to wait for the mousePressed() function.

Nice greetings

1 Like

Welcome to the forum!



int lowerRightCornerX; 

boolean clicked = false; 
//
// ---------------------------------------------------------------
//
void setup() {
  // init
  size(800, 600);
  background(255);

  lowerRightCornerX=286 ;
} // func 

void draw() {
  background(255);
  fill(0); 
  text("please click", 19, 19);
  if (clicked) {
    triangle(130, 175, 
      158, 120, 
      lowerRightCornerX, 175);
  }
} // func 
//

void mousePressed() {
  clicked=true;
}
//
2 Likes

The variable clicked decides what’s shown in draw()

1 Like

Another option is to use loop and noLoop(). Downside is that the first frame of your sketch will still run.

int x;

void setup() {
  size(800, 600);
  noLoop();
  rectMode(CENTER);
  fill(255, 0, 0);
}

void draw() {
  background(255);
  rect(x, height/2, 100, 100);
  x += 10;
}

void mousePressed() {
  loop();
}
1 Like

Thx for your ideas, but noLoop() doesnt stop the program in the middle of draw, i need the program to stop in the middle of draw, it shouldn’t go to the End of draw :), sorry that I didn’t explained that

The program have to wait at one point in draw() for “mousePressed()”, so the way with “boolean clicked” is a good way to see if the mouse is pressed, but with “if(clicked)” you can’t stop the process :wink:

PS: sorry for my bad english, it’s not my mother tongue

2 Likes

It’s true that still draw runs on and on 60 times per second. But it doesn’t matter since with if you can dictate what it draws and what not. So you have control over the screen.

Maybe tell us more what you want to achieve

Actually maybe return can help you:


if( ! clicked) 
      return; // the ! means not
1 Like

here


boolean clicked = false; 
//
// ---------------------------------------------------------------
//
void setup() {
  // init
  size(800, 600);
  background(255);
} // func 

void draw() {

  if (! clicked)
    return;  // leave here 

  background(255);
  fill(0); 
  text("Done", 19, 19);
} // func 
//

void mousePressed() {
  clicked=true;
}
//
2 Likes

Ok first of all, I have to program a german game called “Mensch ärgere dich nicht”. So in my program the variables and comments are in german :confused: but I have a while() - loop in draw(), because I have to use “break;” to restart the loop at a certain point without ending the whole draw().

And in this while() - loop the program should wait for clicked=true :slight_smile:

I am German too and know the game.

Your approach is wrong.

Instead of holding or stopping draw() just let it run on and on, 60 times per second. That’s its job!

Instead of using while or break just have a variable state that dictates what draw() does.

So draw runs on and on again and again but it ignores most parts of itself.

It’s not necessary to let draw “wait” in a while loop (you could with the variable clicked though), instead let draw() run and let state dictate what draw does.

e.g. player enters the From and the To field of a move: This can be two different states so the program knows what to do with the clicked field. But draw runs on and on.

And please make the 50 (+16) fields of the Game Board as buttons in an array.

Chrisir

int lowerRightCornerX; 

final int SPLASH_SCREEN = 0;
final int MOVE          = 1;  
int state = SPLASH_SCREEN;

//
// ---------------------------------------------------------------
//
void setup() {
  // init
  size(800, 600);

  lowerRightCornerX=86 ;
} // func 

void draw() { 
  background(255);

  switch (state) {

  case SPLASH_SCREEN:
    triangle(30, 75, 
      58, 20, 
      lowerRightCornerX, 75);
    fill(0); 
    text("SPLASH_SCREEN !", 
      313, 331);
    break; 

  case MOVE:
    //
    fill(0); 
    text("MOVE", 
      313, 331);
    break;
  }//switch
  //
} // func 
//

void mousePressed() {

  switch (state) {

  case SPLASH_SCREEN:
    state=MOVE;
    break; 

  case MOVE:
    //
    break;
  }//switch
}//func 
//
3 Likes