Sound and Random Shape Typing

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);
  }
}
1 Like

looks like you want the random position each time before you draw the circle??

//https://discourse.processing.org/t/sound-and-random-shape-typing/6877
float circleX;
float circleY;

void setup() {
  size(640, 360);
  background(0);
}

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 'z':
    // only valid key for test
    break;

    // no valid key was pressed, store that information
  default:
    validKey = false;
  }

  if (validKey) {
    stroke(255);
    noFill();
    circleX= random(width); //_______________________________ add here
    circleY= random(height);
    ellipse(circleX, circleY, 55, 55);
  }
}

you understand that i cut down your code to the problem,
as the full code can only be run with library and sound files…
so help next time and do like this,
it is easy to read and easy to test…

1 Like

The problem is simple. You are using circleX and circleY as your “random” position, but the problem is that both are equal to the same single value you set to them in setup(). Every time you call ellipse(circleX, circleY, 55, 55); there is absolutely nothing to change circleX and circleY, so they keep their same values every time you run that function.

To do the thing right, what you’d want to do is to copy the circleX= random(width); and circleY= random(height); part of setup() right before the ellipse(... function, so that every time ellipse(... is called is also the same time circleX and circleY change to new random position, so it gets into that new random position every time.

However, the best approach with how your code is formed is to completely get rid of circleX and circleY values by deleting any lines of code related to them, and just using ellipse(random(width), random(height), 55, 55); instead, as there is no purpose to remember that random value.

Also, for fun, you could also use fill(random(255), random(255), random(255)); instead of noFill(); for random colors! :smiley:

2 Likes

sorry for the uncutted code transcription!
Anyway @Architector_4 has totally solved the problem, thank you all!!