I'm doing a project that has to write different for loops that draw lines and so that when clicked the loop will switch to the next one. As seen in the code each method should appear when the count gets to them and then disappear when the count goes higher. The desired output is when played it should show the first method hbars and when clicked it will go to vbars and clicked again to geoLines and so on, until userdefined which after reaching there it's done. Right now if I play it it will go through all of the methods in a second without me clicking. Sorry about the large amount of code I couldn't really explain the problem without the whole code.
int count=0;
void setup()
{
size(800, 600);
}
void draw()
{
mouseClicked();
}
void hbars()
{
background(0);
fill(255);
for (int y=0; y <height; y+=height/10)
{
rect(0, y, width, height/20);
}
}
void vbars()
{
background(0);
fill(255);
for (int x = 0; x<width; x+=width/8)
{
rect(x, 0, width/16, height);
}
}
void diagonalLines()
{
background(0);
fill(255);
for (int i=0; i<=width*2; i+=width/100)
{
line(0, i, i, 0);
}
}
void geoLines()
{
background(0);
int hx = 0;
int hy = 0;
int vx = width;
int vy = height;
for (int x =0; x < 10; x++)
{
line(0, hy, hx, height);
line(vx, height, 0, vy);
hy+=height/20;
hx+=width/20;
vx-=width/20;
vy-=height/20;
}
}
void circles()
{
background(0);
fill(255);
for (int i=0; i<100; i++)
{
noFill();
circle(width/2, height/2, i*10);
}
}
void fadeRightToLeft(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeLeftToRight(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeDown()
{
background(0);
for (int i=0; i <40; i++) {
if (i%2==1) {
fadeRightToLeft(i*40);
} else
{
fadeLeftToRight(i*40);
}
}
}
void checkerboard()
{
background(0);
int count = 0;
int row = 0;
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (count % 2 == 0)
{
if (row % 2 == 0)
{
fill(255);
} else fill(0);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
} else
{
if (row % 2 == 0)
{
fill(0);
} else fill(255);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
}
}
row++;
}
}
void userDefined()
{
background(0);
for (int i = 0; i<100; i++)
{
stroke(3*i, 160*i, 15*i);
noFill();
circle(0+(i*7.5), 0+(i*7.5), i*12);
circle(0+(i*7.5), 600+(i*7.5), i*40);
}
}
void mouseClicked()
{
count++;
if (count==1)
hbars();
if (count==2)
vbars();
if (count==3)
diagonalLines();
if (count==4)
geoLines();
if (count==5)
circles();
if (count==6)
fadeRightToLeft(0);
if (count==7)
fadeLeftToRight(0);
if (count==8)
fadeDown();
if (count==9)
checkerboard();
if (count==10)
userDefined();
}
You almost had it, you mostly just had to avoid putting the call to mouseClicked()
in the draw()
loop.
Why it didn't work right? The mouseClicked()
method works like an event, which means that it'll be called dynamically when something specific happens (a mouse click in this case). You don't need to call it - as a general rule, you should never call a event handler programatically.
The draw()
loop run about 60 times per second. Since it was calling the method to switch to the next background, after a fraction of a second it ran out of backgrounds.
I fixed your code and modified some details (which I commented so you know why). Here you go:
int count=0;
void setup()
{
size(800, 600);
// I think that we can call the first background before a user input, but it's your call
NextBackground();
}
void draw()
{
// mouseClicked(); // mouseClicked is called as an event, do not call it programatically (also yyou don't need to)
// Processing wants it's draw() loop so we'll let it here even if it's empty.
}
void hbars()
{
background(0);
fill(255);
for (int y=0; y <height; y+=height/10)
{
rect(0, y, width, height/20);
}
}
void vbars()
{
background(0);
fill(255);
for (int x = 0; x<width; x+=width/8)
{
rect(x, 0, width/16, height);
}
}
void diagonalLines()
{
background(0);
fill(255);
for (int i=0; i<=width*2; i+=width/100)
{
line(0, i, i, 0);
}
}
void geoLines()
{
background(0);
int hx = 0;
int hy = 0;
int vx = width;
int vy = height;
for (int x =0; x < 10; x++)
{
line(0, hy, hx, height);
line(vx, height, 0, vy);
hy+=height/20;
hx+=width/20;
vx-=width/20;
vy-=height/20;
}
}
void circles()
{
background(0);
fill(255);
for (int i=0; i<100; i++)
{
noFill();
circle(width/2, height/2, i*10);
}
}
void fadeRightToLeft(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeLeftToRight(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeDown()
{
background(0);
for (int i=0; i <40; i++) {
if (i%2==1) {
fadeRightToLeft(i*40);
} else
{
fadeLeftToRight(i*40);
}
}
}
void checkerboard()
{
background(0);
int count = 0;
int row = 0;
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (count % 2 == 0)
{
if (row % 2 == 0)
{
fill(255);
} else fill(0);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
} else
{
if (row % 2 == 0)
{
fill(0);
} else fill(255);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
}
}
row++;
}
}
void userDefined()
{
background(0);
for (int i = 0; i<100; i++)
{
stroke(3*i, 160*i, 15*i);
noFill();
circle(0+(i*7.5), 0+(i*7.5), i*12);
circle(0+(i*7.5), 600+(i*7.5), i*40);
}
}
void mouseClicked()
{
// I moved this code into it's own method as the mouseClicked method may serve other purposes
NextBackground();
}
void NextBackground() {
count++;
if (count > 10) {
count = 1;
// this way you loop in your backgrounds
}
// a bunch of "if" will get the job done, but you should use a switch for that part:
switch (count) {
case 1:
hbars();
break;
case 2:
vbars();
break;
case 3:
diagonalLines();
break;
case 4:
geoLines();
break;
case 5:
circles();
break;
case 6:
fadeRightToLeft(0);
break;
case 7:
fadeLeftToRight(0);
break;
case 8:
fadeDown();
break;
case 9:
checkerboard();
break;
case 10:
userDefined();
break;
default:
// this one will be used for any other value, which should never happen, but I'm putting it so you know about it
hbars();
}
}
Have fun!