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