MousePressed function not working?

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!

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);
    }
    
  
}
1 Like

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

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

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

2 Likes

Hello,

Initialize:

let daycounter = 0;

Take another look at this:

  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.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.

:)

2 Likes