Hi everyone, I am editing an example of the Processing sound library. I would like that each time that a key has pressed an ellipse will be generated in a random position.
I am stuck because the circle is generated randomly only when a first key is pressed then is always the same because the if condition is related to “validKey” but I don’t know how I could solve it.
Thanks in advance!
import processing.sound.*;
SoundFile[] file;
int numsounds = 5;
float circleX;
float circleY;
void setup() {
size(640, 360);
background(0);
circleX= random(width);
circleY= random(height);
// Create a Sound renderer and an array of empty soundfiles
file = new SoundFile[numsounds];
// Load 5 soundfiles from a folder in a for loop. By naming
// the files 1.aif, 2.aif, 3.aif, ..., n.aif it is easy to iterate
// through the folder and load all files in one line of code.
for (int i = 0; i < numsounds; i++) {
file[i] = new SoundFile(this, (i+1) + ".aif");
}
}
void draw() {
}
void keyPressed() {
// We use a boolean helper variable to determine whether one of the branches
// of the switch-statement was activated or not
boolean validKey = true;
switch(key) {
case 'a':
file[0].play(0.5, 1.0);
break;
case 's':
file[1].play(0.5, 1.0);
break;
case 'd':
file[2].play(0.5, 1.0);
break;
case 'f':
file[3].play(0.5, 1.0);
break;
case 'g':
file[4].play(0.5, 1.0);
break;
case 'h':
file[0].play(1.0, 1.0);
break;
case 'j':
file[1].play(1.0, 1.0);
break;
case 'k':
file[2].play(1.0, 1.0);
break;
case 'l':
file[3].play(1.0, 1.0);
break;
case ';':
file[4].play(1.0, 1.0);
break;
case '\'':
file[0].play(2.0, 1.0);
break;
case 'q':
file[1].play(2.0, 1.0);
break;
case 'w':
file[2].play(2.0, 1.0);
break;
case 'e':
file[3].play(2.0, 1.0);
break;
case 'r':
file[4].play(2.0, 1.0);
break;
case 't':
file[0].play(3.0, 1.0);
break;
case 'y':
file[1].play(3.0, 1.0);
break;
case 'u':
file[2].play(3.0, 1.0);
break;
case 'i':
file[3].play(3.0, 1.0);
break;
case 'o':
file[4].play(3.0, 1.0);
break;
case 'p':
file[0].play(4.0, 1.0);
break;
case 'z':
file[1].play(4.0, 1.0);
break;
// no valid key was pressed, store that information
default:
validKey = false;
}
if (validKey) {
stroke(255);
noFill();
ellipse(circleX, circleY, 55, 55);
}
}