# MousePressed function not working?

**URL:** <https://discourse.processing.org/t/mousepressed-function-not-working/24187>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 28, 2020, 8:08am UTC](https://discourse.processing.org/t/mousepressed-function-not-working/24187 "2020-09-28T08:08:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam101](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@sam101](https://discourse.processing.org/u/sam101)\
**Post date:** [September 28, 2020, 8:08am UTC](https://discourse.processing.org/t/mousepressed-function-not-working/24187/1 "2020-09-28T08:08:39Z")

</div>

Hey everyone, so somehow my mousePressed function isn’t changing the days of the week text whenever I mousepress, but it is starting a new sketch…  
Don’t know whats preventing the text from changing? If anyone can figure out what has gone wrong that would be great!

```auto
var data; // bucket for the notification data
var instagram, snapchat, messenger, facebook, email, instagramCol, snapchatCol, messengerCol, facebookCol, emailCol;
var whichHour=0; //for iterating through data
var sizeMultiplier = 10; 
var time, size, daycounter;
function preload (){
  data =loadJSON("Sunday.json");
}
function setup() {
   createCanvas (windowWidth, windowHeight);
   background (255);
  
   instagramCol = color (142, 68, 173, 90);
   messengerCol = color (133, 193, 233, 90);
   snapchatCol = color (247, 220, 111, 90);
   facebookCol = color (118, 215, 196, 90);
   emailCol = color (178, 186, 187, 90); 
   frameRate(1);
 
   
 textSize (70);
 fill (0);
  textFont ("Futura");
  text ('Sunday',600,600);
  textSize (20);
  text ('Press any key', 150, 380);
  text ('click anywhere on screen',1100, 380);
  textSize(40);
  text ('II',200,350);
  text ('≥', 1200, 350);
  var yplay;
   
   }
function draw () {
  for (whichHour=0; whichHour<data.hours.length; whichHour++){
    fill (instagramCol);
    size = data.hours[whichHour].instagram*sizeMultiplier;
    //ellipse (data[whichHour], whichHour*yplay, whichHour*20, size, size);
    ellipse (random(600, 800), random (150, 300), size, size);
    
    
    fill (messengerCol);
    size = data.hours[whichHour].messenger*sizeMultiplier; 
    ellipse(random(600, 800), random(150, 300), size, size);
    
    fill (snapchatCol);
    size = data.hours[whichHour].snapchat*sizeMultiplier; 
    ellipse(random(600, 800), random(150, 300), size, size);
    
    fill (facebookCol);
    size = data.hours[whichHour].facebook*sizeMultiplier; 
    ellipse(random(600, 800), random(150, 300), size, size);
    
    fill (emailCol);
    size = data.hours[whichHour].email*sizeMultiplier; 
    ellipse(random(600, 800), random(150, 300), size, size);
    }

}

//this allows you to pause the sketch with just pressing any key on the keyboard. 
function keyPressed (){
  noLoop();
}
function keyReleased (){
  loop();
}

function mousePressed (){
  background (255);
  fill (0);
    textSize (70);
    textFont ("Futura");
    
    // changing the title days everytime mouse is pressed
  if (mousePressed) {
    daycounter = daycounter +1;
  }
  if (daycounter > 7) {
    counter = 1;
  }
  
  if (daycounter ==2) {
    text ("Monday", 600, 600);
    }

 if (daycounter ==3) {
    text ("Tuesday", 600,600);
    }
    
    
  if (daycounter ==4) {
    text ("Wednesday", 540,600);
    }  
    
    if (daycounter ==5) {
    text ("Thursday", 600,600);
    }
    
    if (daycounter ==6) {
    text ("Friday", 620,600);
    }
    
    if (daycounter ==7) {
    text ("Saturday", 600,600);
    }
    
  
}

```

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [September 28, 2020, 9:10am UTC](https://discourse.processing.org/t/mousepressed-function-not-working/24187/2 "2020-09-28T09:10:49Z")

</div>

Hello! I’ll answer with a couple of inline comments of your code.

```auto
  if (mousePressed) {
    // When the following line is executed for the first time it will
    // end up with the value NaN. You should declare and set daycounter
    // at the top of your sketch. (var daycounter = 1; or similar.)
    daycounter = daycounter + 1;
  }
  if (daycounter > 7) {
    // You probably want to set daycounter instead of counter here.
    counter = 1;
  }

```

---

<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:** [September 28, 2020, 9:50am UTC](https://discourse.processing.org/t/mousepressed-function-not-working/24187/3 "2020-09-28T09:50:53Z")

</div>

It’s also common to start draw() with background

Then text() in mousePressed would be useless

Instead set a variable in mousePressed and display it in draw()

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 28, 2020, 10:59am UTC](https://discourse.processing.org/t/mousepressed-function-not-working/24187/4 "2020-09-28T10:59:00Z")

</div>

Hello,

Initialize:

`let daycounter = 0;`

Take another look at this:

```auto
  if (daycounter > 7) {
    counter = 1;
  }

```

Add this to mousePressed to see status of variable when developing\debugging:  
` print(daycounter);`

You do not have to check the mousePressed variable inside the mousePressed() function.  
See references:  
[https://p5js.org/reference/#/p5/mousePressed](https://p5js.org/reference/#/p5/mousePressed)  
[https://p5js.org/reference/#/p5.Element/mousePressed](https://p5js.org/reference/#/p5.Element/mousePressed)

These may have been answered in other posts as well; I did not read through them… that is part of my process when I work through code.

I see @Sven made the same observations.

`:)`
