Hello,
I am trying to write a program where if I press the letter ‘c’ on the keyboard a design similar to one that is already programmed for, will appear and continue to go in a circle. The only issue is that the design only appears when I hold the letter ‘c’ on the keyboard and if I don’t hold onto it disappears. I would like the design to stay there once I press ‘c’ on the keyboard.
Things I have tried:
- Move that specific block of code to the void keyPressed () section but it still does not work
- I have tried to code it into a for loop in the following manner but I think I did it wrong. This is because when I press ‘c.’ The program just stops and everything stops moving.
Here is my code below:
void draw() {
background (90);
stroke (255);
strokeWeight(5);
translate(width/2, height/2);
for (int i=0; i< TOTLINES; i++) {
ellipse(x1(t+i), y1(t+i), x2(t+i), y2(t+i));
}
t +=0.5;
if ((keyPressed == true) && (key == 'c')) { // this is the part I need help with
for (int p=0; p< TOTLINES; p++) {
ellipse(x3(t+p), y4(t+p), x5(t+p), y6(t+p));
} //this is the part I need help with
}
}
float x1(float t) {
return sin (t/10) * 130 + sin (t/5)*20;
}
float y1(float t) {
return cos(t / 10)*150;
}
float x2(float t) {
return sin (t/10) * 200 + sin (t)*2;
}
float y2(float t) {
return cos(t / 20)*20 + cos (t/12)*20;
}
float x3(float t) {
return -sin (t/10) * 130 + sin (t/5)*20;
}
float y4(float t) {
return -cos(t / 10)*150;
}
float x5(float t) {
return -sin (t/10) * 200 + sin (t)*2;
}
float y6(float t) {
return -cos(t / 20)*20 + cos (t/12)*20;
}
This is how I tried to code for the issue using a for loop.
I replaced the
if ((keyPressed == true) && (key == 'c')) { // this is the part I need help with
for (int p=0; p< TOTLINES; p++) {
ellipse(x3(t+p), y4(t+p), x5(t+p), y6(t+p));
} //
}
with
for (int i=0;(keyPressed == true) && (key == 'c');i++) { // this is the part I need help with
for (int p=0; p< TOTLINES; p++) {
ellipse(x3(t+p), y4(t+p), x5(t+p), y6(t+p));
} //this is the part I need help with
}
Thank you for taking the time to help me out with this issue! I really appreciate it.