Daft question about variable using mouseX and mouseY

I have a daft question…
why do get no output when I define a variable using mouseX & mouseY?

int XY = mouseX+mouseY;
 
 void draw()
{
 println(XY,mouseX+mouseY);
}

left-hand number stays at zero, right hand number changes as i move the mouse over the window.

It’s a daft question because obviously I could just put mouseX+mouseY wherever I want to return that value… so why declare a variable, except to save a few keystrokes? - but for the sake of understanding I’d like to know!

Hello,

Variable Scope is one reason:
https://processing.org/examples/variablescope.html

:)

1 Like

Thank you! now I see…

 void draw()
{
int XY = (mouseX+mouseY);
  background(mouseX+mouseY);
  println(XY,mouseX+mouseY);
}

both values the same, and background shade responds to the mouse position

int XY = (mouseX+mouseY);
 
 void draw()
{

  background(XY);
  println(XY,mouseX+mouseY);
}

left value stays at zero, background does not respond.

If I’m understanding things correctly, mouseX and mouseY only return a value within draw?

It is initialized before draw() and retains that value until updated.
It is updated in draw().

Look up the references for setup() and draw().

Give this a try:

int t1 = millis();
int t2, t3, t4;

void setup() 
  {
  t2 = millis();
  size(640, 360);
  delay(10);
  t3 = millis();
  }

void draw() 
  {
  t4 = millis();
  if (frameCount == 1) println(t1, t2, t3, t4);
  }
  
void keyPressed()
  {
  println(t1, t2, t3, t4);
  }

:)

1 Like

this is a command and not a standing order. It is only executed when processing executes this line. So it must be within draw() because draw() runs on and on. When it’s before draw() it is executed only once and not throughout (it doesn’t become a rule for processing to maintain the line forever and keep XY as mouseX+mouseY).

1 Like

thanks - that illustrates things really well. Glad to have learnt something!

2 Likes