# Daft question about variable using mouseX and mouseY

**URL:** <https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875>\
**Category:** Beginners\
**Created:** [December 2, 2021, 1:49am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875 "2021-12-02T01:49:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AudioBabble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/audiobabble/32/15092_2.png) [@AudioBabble](https://discourse.processing.org/u/AudioBabble)\
**Post date:** [December 2, 2021, 1:49am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/1 "2021-12-02T01:49:21Z")

</div>

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

```auto
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!

---

<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:** [December 2, 2021, 1:56am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/2 "2021-12-02T01:56:43Z")

</div>

Hello,

Variable Scope is one reason:  
[https://processing.org/examples/variablescope.html](https://processing.org/examples/variablescope.html)

`:)`

---

<div class="post-metadata">

**Author:** ![AudioBabble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/audiobabble/32/15092_2.png) [@AudioBabble](https://discourse.processing.org/u/AudioBabble)\
**Post date:** [December 2, 2021, 2:07am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/3 "2021-12-02T02:07:03Z")

</div>

Thank you! now I see…

```auto
 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

```auto
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?

---

<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:** [December 2, 2021, 2:09am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/4 "2021-12-02T02:09:52Z")

</div>

> [@AudioBabble](#):
>
> 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:

```auto
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);
  }

```

`:)`

---

<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:** [December 2, 2021, 11:31am UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/5 "2021-12-02T11:31:47Z")

</div>

> [@AudioBabble](#):
>
> mouseX and mouseY only return a value within draw?

> [@AudioBabble](#):
>
> `XY = (mouseX+mouseY);`

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

---

<div class="post-metadata">

**Author:** ![AudioBabble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/audiobabble/32/15092_2.png) [@AudioBabble](https://discourse.processing.org/u/AudioBabble)\
**Post date:** [December 2, 2021, 1:15pm UTC](https://discourse.processing.org/t/daft-question-about-variable-using-mousex-and-mousey/33875/6 "2021-12-02T13:15:32Z")

</div>

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