Keypressed strange behaviour : detects all keys despite the conditionals, execute codes it's not supposed to

Hello there. Hopefully I am posting at the right place.

I am trying to get a keyPressed function to work so it changes the mode of my program, thus changing the properties of the moving ellipses shown in the application, but the behaviour of that function is extremely confusing.

  • If I press the F or C keys, those are repeated twice.
  • If I press the N key figuring at the end of the function, it’s only shown once.
  • If I press any other key (who aren’t supposed to do anything), they’re shown once while they shouldn’t.

In addition to that, none of those keys seems to change the properties of my ellipses.

How can I fix all of this ?

ArrayList <Mover> bouncers;
int moverNumvber = 200;
color backgroundColor;

//Change the Color Pattern from 0 to 12.
int colorPattern = 1;

int mode;

//0 = normal
//1 = fat
//2 = crippled


void setup () {
  size (800, 800);
  smooth();

  bouncers = new ArrayList();
  mode = 0;

  //Initial the instance from Mover class
  for (int i = 0; i < moverNumvber; i++) {
    Mover m = new Mover(colorPattern);
    bouncers.add (m);
  }

  background (backgroundColor(colorPattern));
  //setGradientArea(0, 0, width, height, #FD518E, #F7841C, 100);

  frameRate (30);
}


void draw () {

  // To do the fake phantom effect
  fill (backgroundColor(colorPattern), 40);
  //setGradientArea(0, 0, width, height, #FD518E, #F7841C, 100);
  noStroke();
  rect (0, 0, width, height);


  int i = 0;
  while (i < bouncers.size () ) {
    Mover m = bouncers.get(i);
    if (mode == 0)
    {
        bouncers.get(i).ellipseSize = random (4, 15);
        bouncers.get(i).speed = random (5, 10); 
        bouncers.get(i).dispersionMultiplier = 1.2;
    }
    if (mode == 1)
    {
         bouncers.get(i).ellipseSize = random (20, 30);
         bouncers.get(i).speed = random (0.5, 1);
         bouncers.get(i).dispersionMultiplier = 0.5;
    }
    m.flock (bouncers);
    m.move();
    m.checkEdgesInArea ();
    m.display();
    i = i + 1;
  }
}


color backgroundColor(int colorPattern) {

  //0. Default
  if (colorPattern == 0) backgroundColor = #FD518E;
  //1. Sweet
  else if (colorPattern == 1) backgroundColor = #FD518E;
  //2. Oil and fat
  else if (colorPattern == 2) backgroundColor = #FFE400;
  //3. Anxious
  else if (colorPattern == 3) backgroundColor = #000000;
  //4. Disorder
  else if (colorPattern == 4) backgroundColor = #F5515F;
  //5. Scary
  else if (colorPattern == 5) backgroundColor = #040033;
  //6. Bloody
  else if (colorPattern == 6) backgroundColor = #060026;
  //7. Bareen and sad
  else if (colorPattern == 7) backgroundColor = #B2AEAB;
  //8. Calm
  else if (colorPattern == 8) backgroundColor = #145582;
  //9. Over Nutrition
  else if (colorPattern == 9) backgroundColor = #1AAD7E;
  //10. Waste and dirty
  else if (colorPattern == 10) backgroundColor = #4A435F;
  //11. Pollution
  else if (colorPattern == 11) backgroundColor = #2D2D2D;
  //12. Pesticide
  else backgroundColor = #38287F;

  return backgroundColor;
}




//Set background a gradient color
//But it will be slow
void setGradientArea(int x, int y, float w, float h, color c1, color c2, int alpha) {

  noFill();

  for (int i = x; i <= x+w; i++) {
    float inter = map(i, x, x+w, 0, 1);
    color c = lerpColor(c1, c2, inter);
    stroke(c, alpha);
    line(i, y, i, y+h);
  }
}

void keyTyped() 
  {

      if (key == 'f') //Fat and slow.
      {
         print(key); 
         mode = 1;
      }
      else if (key == 'c')
      {
         print(key);
         mode = 2;
      }
      else if (key=='b'); //Normal behaviour
      {
         print(key);
         mode = 0;
      }
    
  }

Thank you by advance for giving me some of your time to fix my issue.

1 Like

You’ve appended a semicolon ; to if () before its opening curly brace {! :scream:

2 Likes

Darn I’m dumb.

Somehow this fixed everything. Thank you so much ! :smiley: