 # Setting a value once by turning off a boolean doesn't work

``````float x1 = random(50, 450);
float y1 = random(50, 450);

int size1 = 100;

float inc1X = 3;
float inc1Y = 3;

float x2 =x1;
float y2 = x1;

int size2 = 50;

float inc2X = 3;
float inc2Y = 3;

boolean place1 = true;

void setup () {
size(500, 500);

}

void draw () {

if (place1 = true) { //supposed to only work once
float x2 =x1;
float y2 = y1;
place1 = false;
}

background(255);
rectMode(CENTER);

fill(120);
rect(x1, y1, size1, size1);
x1+=inc1X;
y1+=inc1Y

if (x1>width-size1/2) {
inc1X = inc1X * -1;
}

if (x1<size1/2) {
inc1X = inc1X * -1;
}

if (y1>height-size1/2) {
inc1Y= inc1Y * -1;
}

if (y1<size1/2) {
inc1Y = inc1Y * -1;
}

fill(255);
rect(x2, y2, size2, size2);
x2+=inc2X;
y2+=inc2Y

}
``````

My code is supposed to set the little square’s position only once to the big square at the beginning, but does it every frame. Can someone help me?

2 Likes

Hmm, I forgot that. I changed it to this:

``````if (place1 == true) {
``````

Still doesnt work though. Now the rect completely disappears.
I tried to print the values x2 and y2 and im getting undefined

1 Like

Not correct to repeat the word float here

Delete both words float

2 Likes

Hello,

An important topic:

`:)`

2 Likes

An old programing trick to avoid the confusion of assignment and equality testing is to invert the order of the test:

`> if (true == myBoolean) doSomething();`

Since you true can’t be assigned a value, this will always throw an error if you get it wrong.

A much better approach is to never directly check for the keywords `true` & `false` at all.

Rather just use the `boolean` variable itself inside `if () {}` w/o comparing it to anything.

Instead of `if (myBoolean == true) {}` just shorten it to just `if (myBoolean) {}`.

And instead of `if (myBoolean == false) {}` prefix the variable w/ the logical not operator `!`:
`if (!myBoolean) {}`.

1 Like