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?