# Question about a bug in my code

**URL:** https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164
**Category:** Coding Questions
**Created:** [April 26, 2020, 5:31am UTC](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164 "2020-04-26T05:31:00Z")
**Posts on this page:** 4
**Page:** 2

<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: [May 1, 2020, 9:35am UTC](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164/21 "2020-05-01T09:35:52Z")

</div>

Yeah, nothing much about this - just my point (1).

I just changed the variable names throughout so that they are different from the names of the functions. Helps for reading the code.

The b in bBing1 stands for boolean.

Also, can you see now you need only one boolean named bBing or secondState?

Can you please post your entire code?

---

<div class="post-metadata">

### Author: ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)
#### Post date: [May 1, 2020, 5:32pm UTC](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164/22 "2020-05-01T17:32:34Z")

</div>

Here is my code, I made some mistake somewhere, like always.  
Thanks

```auto
float a, b, c, d;
int rectX;
int rectY;
void mousePressed() {
  rectX = mouseX;
  if (a > mouseX) a = 0;
  if (b < mouseX) b = width;
  rectY = mouseY;
  if (c > mouseY) c = 0;
  if (d < mouseY) d = height;
}
float x1 = random(a, b);
float y1 = random(c, d);
void bing1() {
  float x1 = random(a, b);
  float y1 = random(c, d);
  if (y1>rectY) {
    d=d-1;//d=100
  }
  if (y1<rectY) {
    c=c+1;//c=1
  }
  if (x1>rectX) {
    b=b-1;//b=100
  }
  if (x1<rectX) {
    a=a+1;//a=1
  }
}
void bing2() {
  if (y1>rectY) {
    y1=y1-1;//d=100
  }
  if (y1<rectY) {
    y1=y1+1;//c=1
  }
  if (x1>rectX) {
    x1=x1-1;//b=100
  }
  if (x1<rectX) {
    x1=x1+1;//a=1
  }
}
boolean bing1 = true;
boolean bing2 = false;
void setup() {
  rectMode(CENTER);
  a = 0;
  b = width;
  c = 0;
  d = height;
  frameRate(10);
  rectX = int(random(0, width));
  rectY = int(random(0, height));
}
void draw() {
  background(#9CA0A0);
  rect(rectX, rectY, 5, 5);
  line(x1, 0, x1, height);
  line(0, y1, width, y1);
  if (bing1 == true) {
    bing1();
  }
  if (x1==rectX && y1==rectY) {
    bing1 = false;
    bing2 = true;
  }
  if (bing2 == true) {
    bing2();
  }
}

```

---

<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: [May 1, 2020, 9:13pm UTC](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164/23 "2020-05-01T21:13:33Z")

</div>

Nice!

In bing1() you had:

```auto
  float x1 = random(a, b);
  float y1 = random(c, d);

```

Repeating the word float makes these new (and local) variables, overshadowing the global variables x1,y1. **That was why you didn’t see the two lines anymore that use x1 and y1.**

Don’t repeat “float” / type when you don’t want the variable to be local!

**Order of things.**

Also, it’s nice to have a certain structure in your Sketches:

- **ALL** global variables
- setup and draw
- Input functions (mousePressed etc.)
- Other functions.

Chrisir

**FULL CODE**

```auto

// ALL global variables 
float a, b, c, d; // the ranges. 
int rectX, rectY; // the rect / target pos
float x1, y1; // the current pos 
boolean bing1 = true; // the behavior flags 
boolean bing2 = false;

// -------------------------------------------------------------------
// Two Core functions 

void setup() {
  size(100, 100); 
  rectMode(CENTER);
  a = 0;
  b = width;
  c = 0;
  d = height;
  frameRate(10);
  rectX = int(random(0, width));
  rectY = int(random(0, height));
}

void draw() {
  background(#9CA0A0);

  rect(rectX, rectY, 5, 5);
  line(x1, 0, x1, height);
  line(0, y1, width, y1);

  // If we reached the target 
  if (x1==rectX && y1==rectY) {
    // switch behavior 
    bing1 = false;
    bing2 = true;
  }

  // call two behaviors
  if (bing1) {
    behavior1();
  }
  if (bing2) {
    behavior2();
  }
}

// -------------------------------------------------------------------
// Input functions 

void mousePressed() {
  rectX = mouseX;
  if (a > mouseX) a = 0;
  if (b < mouseX) b = width;
  rectY = mouseY;
  if (c > mouseY) c = 0;
  if (d < mouseY) d = height;
}

// -------------------------------------------------------------------
// Other functions 

void behavior1() {

  x1 = random(a, b);
  y1 = random(c, d);

  if (y1>rectY) {
    d=d-1;//d=100
  }
  if (y1<rectY) {
    c=c+1;//c=1
  }
  if (x1>rectX) {
    b=b-1;//b=100
  }
  if (x1<rectX) {
    a=a+1;//a=1
  }
}

void behavior2() {
  if (y1>rectY) {
    y1=y1-1;
  }
  if (y1<rectY) {
    y1=y1+1;
  }
  if (x1>rectX) {
    x1=x1-1;
  }
  if (x1<rectX) {
    x1=x1+1;
  }
}
//

```

---

<div class="post-metadata">

### Author: ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)
#### Post date: [May 1, 2020, 9:19pm UTC](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164/24 "2020-05-01T21:19:19Z")

</div>

Thank You very much, I really learned a lot.  
Adi

[Previous page](https://discourse.processing.org/t/question-about-a-bug-in-my-code/20164.md?page=1)
