# Processing mousePressed twice

**URL:** <https://discourse.processing.org/t/processing-mousepressed-twice/34194>\
**Category:** Processing\
**Created:** [December 19, 2021, 12:09pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194 "2021-12-19T12:09:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristina](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kristina](https://discourse.processing.org/u/Kristina)\
**Post date:** [December 19, 2021, 12:09pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194/1 "2021-12-19T12:09:47Z")

</div>

Hi, I wanted to make a code where an Object appears when you press the mouse, which works out fine. But if you press again it should disappear again. Also I have two Objects, and it should always be one that appears. Which one appears is random. But I have no idea how to even start. No matter what I tried if I press again it doesn’t disappear. And how do I set it to random?

Id appreciate some help:)

---

<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:** [December 19, 2021, 12:20pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194/2 "2021-12-19T12:20:49Z")

</div>

Make a variable for each shape.

boolean, when it’s true display the shape otherwise don’t. Use `if(shape1IsOn) …` in draw()

Now in the mousePressed() function switch it to true when false and vice versa

Here you can make a random to decide which one of the 2 variables you change:

`if(random (100)>50) {…} else {…}`

**Remark**

You need setup and draw and at the start of draw background (0); or so

**Full Sketch**

```auto
boolean b1=true, b2=true; 

void setup() {
  size(700, 600);
}

void draw() {
  background(0); 

  if (b1)
    rect(10, 10, 111, 111); 

  if (b2)
    ellipse (202, 210, 111, 111);
}

void mousePressed() {
  if (random(1)<0.5) 
    b1=!b1;
  else 
  b2=!b2;
}

```

---

<div class="post-metadata">

**Author:** ![Kristina](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@Kristina](https://discourse.processing.org/u/Kristina)\
**Post date:** [December 19, 2021, 12:35pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194/3 "2021-12-19T12:35:16Z")

</div>

```auto
Present present;
Tree tree;

boolean appear;

void setup () {

  size (1000, 900);
  background (0);
  textAlign(CENTER);

  present = new Present ();
  tree = new Tree ();
}

void draw () {

  drawAdvent ();
  drawObjects();
}

void drawAdvent () {

  float x = 15;
  float y = 15;

  for (int j = 50; j < height; j += 210) {

    for (int i = 20; i < width; i += 165) {
      fill(20, 50, 150);
      rect(x + i, y + j, 100, 150);

      for (int t = 1; t<25; t++) {

        fill(70, 220, 240);
        textSize(50);
        text(t, x+i+50, y+j +85);
      }
    }
  }
}

void drawObjects () {

  if (mousePressed) {
    appear = true;
  } else {
    appear = false;
  }

  if (appear == true) {
    present.draw();
  }
}

```

```auto
class Present extends christmasObject {

  void draw () {

    translate (b, h);
    drawpresent();
  }

  void drawpresent() {

    scale(0.1);

    beginShape();
    fill(30, 70, 20);
    rect( 150, 400, 500, 400);
    rect ( 125, 350, 550, 100);

    fill(250, 10, 10);

    triangle(400, 340, 300, 200, 225, 300);
    triangle(400, 340, 500, 200, 575, 300);

    ellipse (400, 340, 70, 50);

    rect ( 340, 350, 125, 450);
    endShape();
  }
}

```

```auto
class Tree extends christmasObject {

  void draw () {

    drawtree();
  }

  void drawtree () {
    
    translate(b, h);
    scale(0.2);
    
    beginShape();
    //stem
    rectMode(CENTER);
    fill(90, 50, 20);
    rect(500, 850, 80, 120);

    //Bottom
    fill(0, 50, 0);
    triangle(500, 500, 200, 800, 800, 800);
    //Middle
    fill(0, 60, 0);
    triangle(500, 300, 300, 600, 700, 600);
    //Top
    fill(0, 70, 0);
    triangle(500, 200, 400, 400, 600, 400);
    endShape();
    
  }
}

```

```auto
class christmasObject {

  float b;;
  float h;

  void setup () {
    b = mouseX;
    h = mouseY;
  }
  
  
  
  void draw () {
  }
}

```

This is my Code so far but I am so lost right now. Its supposed to be a little Advent Calendar for fun.  
But the present Always appears at the same position and I don’t really understand how I should add what you suggested 😕

Also lets not talk about my numbers on the doors I still gotta figure that out too haha

---

<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:** [December 19, 2021, 3:08pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194/4 "2021-12-19T15:08:13Z")

</div>

without looking at your code I posted an example above

Now I look at your code…

---

<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:** [December 19, 2021, 3:25pm UTC](https://discourse.processing.org/t/processing-mousepressed-twice/34194/5 "2021-12-19T15:25:28Z")

</div>

here is an example.

Instead of using mousePressed as a variable in draw() I use the function  
mousePressed().  
Here I change b,h (normally x,y) and the boolean `appearPresent` OR `appearTree`

**Remark**

also when using translate or scale we want to use pushMatrix and popMatrix to isolate it. Otherwise hey add up.

**Full Sketch**

```auto

Present present;
Tree tree;

boolean appearPresent=true;
boolean appearTree=true;

void setup () {

  size (1000, 900);
  background (0);
  textAlign(CENTER);

  present = new Present ();
  tree = new Tree ();
}

void draw () {
  background (0);

  drawAdvent ();
  drawObjects();
}

//---------------------------------------------------------------------------

void drawAdvent () {
  float x = 15;
  float y = 15;

  int dayNumber=1;
  for (int j = 50; j < height; j += 210) {
    for (int i = 20; i < width; i += 165) {
      fill(20, 50, 150);
      rect(x + i, y + j, 100, 150);

      // for (int t = 1; t<13; t++) {
      fill(70, 220, 240);
      textSize(50);
      text(dayNumber, x+i+50, y+j +85);
      // }
      dayNumber++;
    }
  }
}// func 

void drawObjects () {
  if (appearPresent) {
    present.draw();
  }
  if (appearTree) {
    tree.draw();
  }
}

void mousePressed() {
  if (random(1)<0.5) {
    appearPresent = ! appearPresent;
    present.b=random(width-50); 
    present.h=random(height-50);
  } else {
    appearTree = ! appearTree;
    tree.b=random(width-50); 
    tree.h=random(height-50);
  }
}

//===

class Present extends christmasObject {

  void draw () {
    drawpresent();
  }

  void drawpresent() {
    pushMatrix();
    translate (b, h);

    scale(0.1);

    beginShape();
    fill(30, 70, 20);
    rect( 150, 400, 500, 400);
    rect ( 125, 350, 550, 100);

    fill(250, 10, 10);

    triangle(400, 340, 300, 200, 225, 300);
    triangle(400, 340, 500, 200, 575, 300);

    ellipse (400, 340, 70, 50);

    rect ( 340, 350, 125, 450);
    endShape();
    popMatrix();
  }//method
}//class

//=====

class Tree extends christmasObject {
  void draw () {
    drawtree();
  }

  void drawtree () {
    pushMatrix();
    translate(b, h);
    scale(0.2);

    beginShape();
    //stem
    rectMode(CENTER);
    fill(90, 50, 20);
    rect(500, 850, 80, 120);

    //Bottom
    fill(0, 50, 0);
    triangle(500, 500, 200, 800, 800, 800);
    //Middle
    fill(0, 60, 0);
    triangle(500, 300, 300, 600, 700, 600);
    //Top
    fill(0, 70, 0);
    triangle(500, 200, 400, 400, 600, 400);
    endShape();
    popMatrix();
  }
}

// ===

class christmasObject {

  float b;
  float h;

  void setup () {
    b = random(width-50); 
    h = random(height-50);
  }

  //void draw () {
  //}
}
//

```
