Hello, I’ve written some code that moves a rectangle and triangle (an arrow) to move left to right but I’m positive that there is a cleaner and more efficient way to do this. My code for this was a bit unorganised and I was wondering if there was a better way to do this. Any help would be greatly appreciated
Here is my code:
float speedRect = 3;
float speedTri = 3;
float rectx = 50;
float trix1 = 25;
float trix2 = 65;
float trix3 = 105;
void setup(){
size(500, 500);
}
void draw(){
background(100);
rectMode(CORNER);
fill(50);
stroke(50);
ellipse(400, 100, 25, 25);
rect(rectx, 350, 30, 100);
triangle(trix1, 350, trix2, 300, trix3, 350);
rectx = rectx + speedRect;
trix1 = trix1 + speedTri;
trix2 = trix2 + speedTri;
trix3 = trix3 + speedTri;
if(rectx + 28 > width || rectx < 0) {
speedRect = speedRect * -1.0;
}
if(trix1 + 25 < 0) {
speedTri = speedTri * -1.0;
}
if(trix2 > width || trix2 < 0) {
speedTri = speedTri * -1.1;
}
if(trix3 - 25 > width || trix3 < 0) {
speedTri = speedTri * -1.0;
}
}