# Waiting for mousePressed() in draw

**URL:** <https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795>\
**Category:** Coding Questions\
**Created:** [October 19, 2019, 10:45am UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795 "2019-10-19T10:45:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefan\_98](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@stefan\_98](https://discourse.processing.org/u/stefan_98)\
**Post date:** [October 19, 2019, 10:45am UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/1 "2019-10-19T10:45:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 19, 2019, 10:50am UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/2 "2019-10-19T10:50:04Z")

</div>

Welcome to the forum!

```auto

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;
}
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 19, 2019, 12:13pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/3 "2019-10-19T12:13:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [October 19, 2019, 2:16pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/4 "2019-10-19T14:16:24Z")

</div>

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

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![stefan\_98](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@stefan\_98](https://discourse.processing.org/u/stefan_98)\
**Post date:** [October 20, 2019, 4:42pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/5 "2019-10-20T16:42:01Z")

</div>

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 😉

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 20, 2019, 5:21pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/6 "2019-10-20T17:21:09Z")

</div>

> [@stefan\_98](#):
>
> 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

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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 20, 2019, 6:47pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/7 "2019-10-20T18:47:16Z")

</div>

here

```auto

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;
}
//

```

---

<div class="post-metadata">

**Author:** ![stefan\_98](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@stefan\_98](https://discourse.processing.org/u/stefan_98)\
**Post date:** [October 21, 2019, 4:43pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/8 "2019-10-21T16:43:35Z")

</div>

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 😕 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 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 21, 2019, 5:39pm UTC](https://discourse.processing.org/t/waiting-for-mousepressed-in-draw/14795/9 "2019-10-21T17:39:49Z")

</div>

> [@stefan\_98](#):
>
> 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().

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

```auto
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 
//

```
