# Hover color change

**URL:** https://discourse.processing.org/t/hover-color-change/12169
**Category:** Beginners
**Created:** [June 18, 2019, 7:03pm UTC](https://discourse.processing.org/t/hover-color-change/12169 "2019-06-18T19:03:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![srishtiverma](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@srishtiverma](https://discourse.processing.org/u/srishtiverma)
#### Post date: [June 18, 2019, 7:03pm UTC](https://discourse.processing.org/t/hover-color-change/12169/1 "2019-06-18T19:03:07Z")

</div>

Hello! I’m a Processing beginner.

Trying to make (a few) circles and rectangles in the 100 pixel radius of the mouse coordinates change color (and maybe even move, once I have the color change down)

It says “b cannot be resolved to a variable”. I can’t tell why. How could I go about this better?

My code so far:

```auto
size(600, 600);
noStroke();
background(255);
fill(250);

int a = 0;
int c = 0;
while (a < width) 
while (c < width){
int b = 0;
int d = 0;
while(b < height)
while(d < height){
  
  rect(a, b-15, 70, 30);
  rect(c-15, d, 30, 70);
  b= b + 70;
  d= d + 70;
  }
  
  a= a + 70;
  c= c + 70;
  
}

int x = 0;
while (x < width) {
  int y = 0;
  while(y < height) {
    fill(240);
  ellipse(x, y, 30, 30);
  y= y + 70;
  }
  x= x + 70;
}

if( dist(a, b, mouseX, mouseY) < 20 ){
      fill(0);
} else {
      fill(250);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 18, 2019, 7:15pm UTC](https://discourse.processing.org/t/hover-color-change/12169/2 "2019-06-18T19:15:13Z")

</div>

- Local variable _b_ is declared inside the `while (c < width) {` loop block: `int b = 0;`
- And then you attempt to access it at `if (dist(a, b, mouseX, mouseY) < 20) {`.
- Which is already outside the `while (c < width) {` loop block.
- In Java, variables cease to exist when it’s outta scope.

---

<div class="post-metadata">

### Author: ![srishtiverma](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@srishtiverma](https://discourse.processing.org/u/srishtiverma)
#### Post date: [June 19, 2019, 5:15am UTC](https://discourse.processing.org/t/hover-color-change/12169/3 "2019-06-19T05:15:47Z")

</div>

I tried to use what I understood from this and wrote this instead. No error now, but the colour still doesn’t change on hovering. Not sure I understand why.

```auto
int a = 0;
int b = 0;
int c = 0;
int d = 0;

void setup(){
size(450, 850);
noStroke();
frameRate(15);
background(255);
}

//fill(250);
//vertical and horizontal rectangles

void draw(){
while (a < width) 
while (c < width){
b = 0;
d = 0;
while(b < height)
while(d < height){
  fill(random(150,255), 220, 220);
  rect(a, b-15, 70, 30);
  rect(c - 15, d, 30, 70);
  b= b + 70;
  d= d + 70;
  }
  
  a= a + 70;
  c= c + 70;
  
}

//circles

int x = 0;
int y = 0;
while (x < width) {
y = 0;
  while(y < height) {
    fill(255);
  ellipse(x, y, 30, 30);
  y= y + 70;
  }
  x= x + 70;
}
  if ( dist(x, y, mouseX, mouseY) < 20 ){
      fill(0);
  } else {
      fill(250);
  }

  if ( dist(x, y, mouseX, mouseY) < 20 ){
      fill(0);
  } else {
      fill(250);
  }

}

```

---

<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: [June 19, 2019, 3:04pm UTC](https://discourse.processing.org/t/hover-color-change/12169/4 "2019-06-19T15:04:19Z")

</div>

Click ctrl-t to improve your indents automatically

Then check how far the while loops reach

Hint: the if must be inside the while loops }

and also before ellipse
