# Why a= a* -1 doesn't work here?

Hi, I just made a rectangle which moves along the width and height of the window. I managed to make one but during the process, i encountered a problem that ‘a = a * -1’ can’t work. I couldn’t figure it out the reason.
here is the cold that works.

``````int x=0;
int y=0;
int xspeed;
int yspeed;

void setup(){
size(400,400);
}

void draw(){
background(0);

//display a rect
fill(255);
rect(x,y,20,20);
x = x + xspeed;
y = y + yspeed;

//state changing

if(x == 0 && y == 0){
xspeed = 1;
yspeed = 0;
}else if(x == 380 && y == 0){
xspeed = 0;
yspeed = 1;
}else if(x == 380 && y == 380){
xspeed = -1;
yspeed = 0;
}else if(x == 0 && y == 380){
xspeed = 0;
yspeed = -1;

}
}
``````

but if i change int xspeed into int xspeed =1 and xspeed = -1 into xspeed = xspeed * -1, the rectangle would stuck there under that if statement. i don’t understand why it doesn’t work as the xspeed in both codes are equal to -1.
here is the code with this problem.

``````int x=0;
int y=0;
int xspeed  = 1;
int yspeed;

void setup(){
size(400,400);
}

void draw(){
background(0);

//display a rect
fill(255);
rect(x,y,20,20);
x = x + xspeed;
y = y + yspeed;

//state changing

if(x == 0 && y == 0){
xspeed = 1;
yspeed = 0;
}else if(x == 380 && y == 0){
xspeed = 0;
yspeed = 1;
}else if(x == 380 && y == 380){
xspeed = xspeed *-1;
yspeed = 0;
}else if(x == 0 && y == 380){
xspeed = 0;
yspeed = -1;

}
}
``````

What’s wrong with a= a*-1 here?

0 * -1 = 0

keep that in mind

but i already set int xspeed = 1 at the beginning of the code
so it should be xspeed = 1 *-1 = -1, not 0

when the square comes to bottom right corner, its x speed is 0, when you tell it *= -1, it will stay at 0

ahhh i got it now. it becomes 0 from the last if statement. Thank you so much!

2 Likes