I am using processing and I am trying to get the rectangle to reverse its direction when it hits the side of the screen. Specifically, when the edge of the rectangle reaches the side of the window, the rectangle should change direction and start moving towards the other side instead of still going right. The direction should reverse again when it hits the other side of the window.
ArrayList<Ball> balls;
int rectWidth = 20;
int rectHeight = 20;
long lastTime = 0;
long lastTimeEllipse = 0;
float tx = 0; //x value TimeLine
float tx1 = 0; //x value Ellipse
float value = 0;
void setup() {
size(400, 200);
frameRate(60);
balls = new ArrayList();
lastTime = millis();
lastTimeEllipse = millis();
}
void draw() {
background(0);
if ( millis() - lastTime > 500) {
lastTime = millis();
//after 0.5 sec. tx moves 40px to the right
tx += 40;
value = 2;
} else if (millis()-lastTime < 500) {
value = 1;
}
stroke(255, 0, 0);
line(tx, 0, tx, height);
if (tx>=width) {
tx=0;
tx1 = tx1 + width;
}