Different background, Different item by mousePressed

please format code with </> button * homework policy * asking questions

I’m making day/night animation where I use “mousePressed” on locating the sun on the blue sky according to the mouseX,mouseY. When the key ‘n’ is pressed, the sky turns to dark and the sun disappears. So, HOW to replace the sun with a moon image in the night sky and locate it the same way as the sun using the “mousePressed” and mouseX/Y??

Here’s what I did:

boolean sun = false;
int sunX; 
int sunY;
boolean night = false;

void setup() {
  size(400, 400);

  ellipseMode(CENTER);
}

void draw() {

  if (night == true) {
    background(0, 0, 80);
  } else {
    background(0, 200, 200);
  }

  if (sun == true) {
    fill(255, 255, 0);
    ellipse(sunX, sunY, 80, 80);
  }
}

void mousePressed() {
  sun = true;  
  sunX = mouseX;
  sunY = mouseY;
}

void keyPressed() {
  if (key=='n') {
    night = true;
    sun = false;
  }  
}

Welcome to the forum.

mouseX and mouseY values are always available, not just in mouse events. So you can store them when n key pressed.

void keyPressed() {
  if (key=='n') {
    night = true;
    sun = false;
    moonX = mouseX;
    moonY = mouseY;
  }  
}
1 Like