Arrays, recording information
int[] xcoords= new int[100];
int count=0;
void setup()
{
size(800,600);
}
void mousePressed()
{
xcoords[count]=mouseX;
count++;
}
void draw()
{
int i; // index variable for the loop
float opacity; // a variable for random opacity
background(255);
noFill();
for (i=0;i < count; i++)
{
opacity = random(255);
stroke(0,0,0,opacity);
ellipse(xcoords[i],300,40,40);
}
}
Exercises
- add more random variables so that each ellipse is a random size and color
- add another array to store the Y Coordinates of each ellipse. Save the mouse position in it when you click, and use it when drawing the ellipses.
- note that after 100 clicks the program crashes. This is because it's beyond the limit of the array. Put an if statement in to prevent this, so that count can't get beyond 100.