Processing mousePressed twice

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:)

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

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

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();
  }
}
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();
  }
}
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();
    
  }
}
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 :confused:

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

1 Like

without looking at your code I posted an example above

Now I look at your code…

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


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 () {
  //}
}
//