Question about a bug in my code

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?

1 Like

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

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();
  }
}
1 Like

Nice!

In bing1() you had:

  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


// 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;
  }
}
//
1 Like

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

1 Like