I'm pretty new to Processing. I'm trying to draw a line from the last rectangle my controlled "ball" was over, to the next rectangle it passes over. I've almost got it, but can't figure out how I should rearrange the code to make the lines last, and not be covered up by the redrawing of the background, or be drawn at every instance of draw.
Here's what I have so far. Just testing it with the first two rectangles:
int radius, directionX, directionY, x1;
int previousX, previousY, currentX, currentY, a, b, c, d;
float x, y, speed;
void setup () {
size (500,500);
smooth ();
noStroke();
background(255);
directionY = 0;
directionX = 1;
radius = 30;
previousX = -1;
previousY = -1;
currentX = 0;
currentY = 0;
a = 20;
b = 20;
c = 20;
d = 20;
x= 30;
y= 30;
speed = 2;
}
void draw () {
int n1= 75;
int n2= 325;
int l= 100;
background(255);
stroke(4);
fill(25,81,139,a);
rect(n1,n1,l,l);
if((x>n1) && (x<(n1+100)) && (y>n1) && (y<n1+100)){
a=a+ 1;
fill(25,81,139,a);
rect(n1,n1,l,l);
currentX = 125;
currentY = 125;
if(previousX>0 && previousY>0) {
line(previousX,previousY,currentX, currentY);
}
}
previousX = 125;
previousY = 125;
fill(25,81,139,b);
rect(n1,n2,l,l);
if((x>n1) && (x<(n1+100)) && (y>(n2)) && (y<(n2+100))){
b=b+ 1;
fill(25,81,139,a);
rect(n1,n2,l,l);
currentX = 125;
currentY = 325;
if(previousX>0 && previousY>0){
line(previousX,previousY,currentX, currentY);
}
previousX = 125;
previousY = 325;
}
fill(25,81,139,c);
rect(n2,n1,l,l);
fill(25,81,139,d);
rect(n2,n2,l,l);
x=x+speed*directionX;
y=y+speed*directionY;
//boundaries
if ((x>width-radius) || (x<radius)){
directionX= -directionX;}
if ((y>height-radius) || (y<radius)){
directionY= -directionY;}
fill(255);
stroke(0);
strokeWeight(1);
ellipse(x,y,radius,radius);
}
void keyPressed() //movement ===========================================================
{
if (key == CODED)
{
if (keyCode == DOWN)
{
directionX=0;
directionY=1;
}
else if (keyCode == UP)
{
directionX=0;
directionY=-1;
}
else if (keyCode == LEFT)
{
directionX= -1;
directionY= 0;
}
else if (keyCode == RIGHT)
{
directionX= 1;
directionY= 0;
}
}
}