Hi there -
I’m struggling with a problem that I was hoping someone here could help with.
I wrote some code - provided below - where the “a” key on the keyboard makes some rectangles and the “b” key makes some circles.
I’d ilke to have a functionality by which “a” and “b” could be held down at the same time and rectangles and circles would be drawn.
Does anyone have any suggestions on how to do this?
Thanks!
int in = 0;
void setup() {
size(400, 400);
background(100, 200, 0);
}
void draw() {
if (in == 1)
{aMove();}
else if (in == 2)
{bMove();}
else {background(0,0, 0);}
}
void aMove(){
fill(100, 200, 0);
rect(random(100)+100, random(100)+100, random(100), random(100));
}
void bMove(){
fill(0, 200, 200);
circle(random(100), random(100), random(100));
}
void keyPressed(){
if (key == 'a') {in = 1;}
else if (key == 'b') {in = 2;}
else {in = 3;}
}
I followed the first example and that works at allowing the keystroke to turn things on and off - but still not simultaneously (code below)… If I were to add && statements for every possible combination of every key I’m planning to use, it would be an enormous amount of code… Is there something I’m missing?
boolean[] yawn = new boolean[27];
void setup(){
size(800, 800);
for (int x = 0; x<27; x++)
{yawn[x] = false;}
}
void draw(){
if (yawn[0] == true)
{aMove();}
else if (yawn[1] == true)
{bMove();}
// else {background(0,0, 0);}
}
void aMove(){
fill(100, 200, 0);
rect(random(100)+100, random(100)+100, random(100), random(100));
}
void bMove(){
fill(0, 200, 200);
circle(random(100), random(100), random(100));
}
void keyPressed(){
for (int numLet = 0; numLet<27; numLet++)
{
if (key==numLet+97) {yawn[numLet]=true;}
}
}
void keyReleased(){
for (int numLet = 0; numLet<27; numLet++)
{
if (key==numLet+97) {yawn[numLet]=false;}
}
}
Thanks, @scudly - this is a very elegant solution. I’m noticing that it stops working after four or so keys have been depressed, though - do you know if there is a reason for that?