# Box just going off screen and then disappears. Please help

So i have a box that goes across the screen and is is supposed to loop but it just does it once

Broken Part

if (boxX+10 ==(width)) {

``````boxX=width-width+1;
``````

}

boxX = boxX+10;
rect(boxX, boxY, 50, 50);
}

Full Code

int carX = 50;
int carY= 50;
color carRed = color(255, 0, 0);
color tires = color(0, 0, 0);
boolean vertical = false;//helps with drawing
int speed=30;
boolean vertical2 = false;
int carXX = 50;
int carYY= 50;
color carBlue = color(0, 0, 255);
int tireX=(50);
int tireY=(50);
int boxX=(100);
int boxY=(100);
void setup() {
//size(900,900);
fullScreen();
}

void draw() {
background(10, 225, 50) ;
if (vertical == true) {
fill(carRed);
rect(carX-10, carY-25, 20, 50);
fill(tires);
rect(carX+10, carY-15, 5, 10);
rect(carX-15, carY+10, 5, 10);
rect(carX+10, carY+10, 5, 10);
rect(carX-15, carY-15, 5, 10);
} else {
fill(carRed);
rect(carX-25, carY-10, 50, 20);
fill(tires);
rect(carX+10, carY-15, 10, 5);
rect(carX-15, carY+10, 10, 5);
rect(carX+10, carY+10, 10, 5);
rect(carX-15, carY-15, 10, 5);
}

if (vertical2 == true) {
fill(carBlue);
rect(carXX-10, carYY-25, 20, 50);
fill(tires);
rect(carXX+10, carYY-15, 5, 10);
rect(carXX-15, carYY+10, 5, 10);
rect(carXX+10, carYY+10, 5, 10);
rect(carXX-15, carYY-15, 5, 10);
} else {
fill(carBlue);
rect(carXX-25, carYY-10, 50, 20);
fill(tires);
rect(carXX+10, carYY-15, 10, 5);
rect(carXX-15, carYY+10, 10, 5);
rect(carXX+10, carYY+10, 10, 5);
rect(carXX-15, carYY-15, 10, 5);
}
if (carX>=displayWidth) {

``````carX=displayWidth-displayWidth+1;
``````

}
if (carXX>=displayWidth) {

``````carXX=displayWidth-displayWidth+1;
``````

}
if (carY>=displayHeight) {

``````carY=displayHeight-displayHeight+1;
``````

}
if (carYY>=displayHeight) {

``````carYY=displayHeight-displayHeight+1;
``````

}
if (carY<=0) {

``````carY=displayHeight+0-1;
``````

}
if (carYY<=0) {

``````carYY=displayHeight+0-1;
``````

}
if (carX<=0) {

``````carX=displayWidth+0-1;
``````

}
if (carXX<=0) {

``````carXX=displayWidth+0-1;
``````

}

if (boxX+10 ==(width)) {

``````boxX=width-width+1;
``````

}

boxX = boxX+10;
rect(boxX, boxY, 50, 50);
}

// if (boxX == carX);
// {
// if (boxY == carY);
// {

// speed = 0;
// }
// }

void keyPressed() {

if (keyCode==87) {//W
vertical=true;
carY -=speed;
} else if (keyCode==65) {//A
vertical=false;
carX-=speed;
} else if (keyCode==68) {//D
vertical=false;
carX+=speed;
} else if (keyCode==83) {//S
vertical=true;
carY+=speed;
}

if (keyCode==38) {//W
vertical2=true;
carYY -=speed;
} else if (keyCode==37) {//A
vertical2=false;
carXX-=speed;
} else if (keyCode==39) {//D
vertical2=false;
carXX+=speed;
} else if (keyCode==40) {//S
vertical2=true;
carYY+=speed;
} else if (keyCode==192) {//S
print(â€śSpeed=â€ť+speed);
}

if (carY==carYY && carX==carXX) {
if (carXX==carX && carYY==carY) {
rect(10, 10, 1000, 1000);
}
}

print("key: "+key+â€ścode: â€ś+keyCode+â€ť\nâ€ť);
}

In the second round you start at 1 and add 10 each cycle. So boxX will never be exactly equal width.
You want to check for equal or larger width of boxX.

1 Like