Attempting to using If statements in cases

Hello all, I'm currently working on a project where when you roll the mouse wheel the image/shape beings to animate. To explain this here is the current code I have produced

int num = 0;
float e = 0;
int scroll = 0;

void setup() {
  size(700, 650);
}


void draw() { 
  ellipseMode(CENTER);
    ellipse(350, 325, 100, 100);

    }
void mouseWheel(MouseEvent event) {
  float e = (event.getAmount() + 1);
  if(e == 2.0)
  scroll = scroll + 1;
  println(scroll);

//Scroll starter
//--------------

  if(scroll == 1){
    ellipse(350, 325, 100, 100);
    fill(#bde3db);
  }
  else if(scroll == 2){
    ellipse(350, 325, 100, 100);
    fill(#9ee6d8);
  }
  else if(scroll == 3){
    ellipse(350, 325, 100, 100);
    fill(#41dac5);
  }
  else if(scroll == 4){
    ellipse(350, 325, 100, 100);
    fill(#38c8b4);
  }
  else if(scroll == 5){
    ellipse(350, 325, 100, 100);
    fill(#25a997);
  }
  else if(scroll == 6){
    ellipse(350, 325, 100, 100);
    fill(#1c9786);
  }
  else if(scroll == 7){
    ellipse(350, 325, 100, 100);
    fill(#158575);
  }
  else if(scroll == 8){
    ellipse(350, 325, 100, 100);
    fill(#0c6c5e);
    }
  else if(scroll == 9){
    ellipse(350, 325, 100, 100);
    fill(#054d43);
  }
  else if(scroll == 10){
    ellipse(350, 325, 100, 100);
    fill(#023931);
  }
  else if(scroll == 11){
    ellipse(350, 325, 100, 100);
    fill(#012520);
  }
  else if(scroll == 12){
    ellipse(350, 325, 100, 100);
    fill(#000c0a);
}
}

The way this works is that it takes the value from the scroll wheel and converts it in to a number. This number starts to increase when you continue to scroll down. In this example when you scroll down, the green ellipse becomes a darker shade of green.

Now here's the problem. I am trying to make the program randomly decide between three different scroll sequences at the start to go with, but when I start scrolling the mouse the colours start to either get jumbled up or stop changing. In this program I have case 1 featuring the original green ellipse, in the second case I have a red ellipse and in the third case, a blue ellipse.

int num = 0;
float e = 0;
int scroll1 = 0;
int scroll2 = 0;
int scroll3 = 0;

void setup() {
  size(700, 650);
}


void draw() { 
  ellipseMode(CENTER);
    ellipse(350, 325, 100, 100);

    }
void mouseWheel(MouseEvent event) {
  float e = (event.getAmount() + 1);
  if(e == 2.0)
  scroll1 = scroll1 + 1; //This makes sure that the scrolls ints are connected to the mouse roll
  scroll2 = scroll2 + 1;
  scroll3 = scroll3 + 1;
  println(scroll1);
  println(scroll2);
  println(scroll3);

//Scroll sequence
//--------------
  num = int(random(3));
  switch(num) {

  case 1: //Green Scroll Sequence
  scroll2 = 0 - 1000;
  scroll3 = 0 - 1000;

  if(scroll1 == 1){
    ellipse(350, 325, 100, 100);
    fill(#bde3db);
  }
  else if(scroll1 == 2){
    ellipse(350, 325, 100, 100);
    fill(#9ee6d8);
  }
  else if(scroll1 == 3){
    ellipse(350, 325, 100, 100);
    fill(#41dac5);
  }
  else if(scroll1 == 4){
    ellipse(350, 325, 100, 100);
    fill(#38c8b4);
  }
  else if(scroll1 == 5){
    ellipse(350, 325, 100, 100);
    fill(#25a997);
  }
  else if(scroll1 == 6){
    ellipse(350, 325, 100, 100);
    fill(#1c9786);
  }
  else if(scroll1 == 7){
    ellipse(350, 325, 100, 100);
    fill(#158575);
  }
  else if(scroll1 == 8){
    ellipse(350, 325, 100, 100);
    fill(#0c6c5e);
    }
  else if(scroll1 == 9){
    ellipse(350, 325, 100, 100);
    fill(#054d43);
  }
  else if(scroll1 == 10){
    ellipse(350, 325, 100, 100);
    fill(#023931);
  }
  else if(scroll1 == 11){
    ellipse(350, 325, 100, 100);
    fill(#012520);
  }
  else if(scroll1 == 12){
    ellipse(350, 325, 100, 100);
    fill(#000c0a);
    noLoop();
  }
break;

//-------------

  case 2: //Red Scroll Sequence
  scroll1 = 0 - 1000;
  scroll3 = 0 - 1000;

  if(scroll2 == 1){
    ellipse(350, 325, 100, 100);
    fill(#fff3f5);
  }
  else if(scroll2 == 2){
    ellipse(350, 325, 100, 100);
    fill(#fedadf);
  }
  else if(scroll2 == 3){
    ellipse(350, 325, 100, 100);
    fill(#fdc6ce);
  }
  else if(scroll2 == 4){
    ellipse(350, 325, 100, 100);
    fill(#fab2bc);
  }
  else if(scroll2 == 5){
    ellipse(350, 325, 100, 100);
    fill(#f393a1);
  }
  else if(scroll2 == 6){
    ellipse(350, 325, 100, 100);
    fill(#ea7a8a);
  }
  else if(scroll2 == 7){
    ellipse(350, 325, 100, 100);
    fill(#e36879);
  }
  else if(scroll2 == 8){
    ellipse(350, 325, 100, 100);
    fill(#da5668);
    }
  else if(scroll2 == 9){
    ellipse(350, 325, 100, 100);
    fill(#c7374b);
  }
  else if(scroll2 == 10){
    ellipse(350, 325, 100, 100);
    fill(#be253a);
  }
  else if(scroll2 == 11){
    ellipse(350, 325, 100, 100);
    fill(#611927);
  }
  else if(scroll2 == 12){
    ellipse(350, 325, 100, 100);
    fill(#421c24);
    noLoop();
break;
}
//-------------

  case 3: //Blue Scroll Sequence
  scroll1 = 0 - 1000;
  scroll2 = 0 - 1000;

  if(scroll3 == 1){
    ellipse(350, 325, 100, 100);
    fill(#cbecff);
  }
  else if(scroll3 == 2){
    ellipse(350, 325, 100, 100);
    fill(#b0dcf6);
  }
  else if(scroll3 == 3){
    ellipse(350, 325, 100, 100);
    fill(#9acdec);
  }
  else if(scroll3 == 4){
    ellipse(350, 325, 100, 100);
    fill(#81bde2);
  }
  else if(scroll3 == 5){
    ellipse(350, 325, 100, 100);
    fill(#5fa3cc);
  }
  else if(scroll3 == 6){
    ellipse(350, 325, 100, 100);
    fill(#4088b3);
  }
  else if(scroll3 == 7){
    ellipse(350, 325, 100, 100);
    fill(#2c6e96);
  }
  else if(scroll3 == 8){
    ellipse(350, 325, 100, 100);
    fill(#205a7e);
    }
  else if(scroll3 == 9){
    ellipse(350, 325, 100, 100);
    fill(#0f405f);
  }
  else if(scroll3 == 10){
    ellipse(350, 325, 100, 100);
    fill(#092e46);
  }
  else if(scroll3 == 11){
    ellipse(350, 325, 100, 100);
    fill(#041d2d);
  }
  else if(scroll3 == 12){
    ellipse(350, 325, 100, 100);
    fill(#010d14);
    noLoop();
break;
}
}
}

I have tried adding two minus 1000's at the beginning to try and stop the sequences from overlapping, but I am having no luck. Does anyone have any alternatives?

Answers

  • edited February 2014

    I think you want this line

    num = int(random(3));

    in draw()

    so it doesn't get called so oft

  • Apart from other comments that I could make on your code...

    e.g. why is fill after ellipse? It should be before it...

  • Also I would use

    while (scroll>12)
       scroll=scroll-11;
    

    because you need to check values > 12

    shouldn't you say

    scroll1=e; 
    

    ?

  • I'll give the while statement a shot.

    I did try scroll1=e, but the scroll value does not go up.

  • edited February 2014

    given that all the ellipse calls are identical they (it) can be outside the conditional parts

    also, read up on Arrays - your colours could all be in an array, defined at the start of the code, and then just use the one relevant to the current value of scroll variable. means you can replace the whole thing with maybe 10 lines of code.

    generally, if you have lots of code that looks very similar then you're doing it wrong.

  • edited February 2014

    also, this:

      if(e == 2.0)
      scroll1 = scroll1 + 1; //This makes sure that the scrolls ints are connected to the mouse roll
      scroll2 = scroll2 + 1;
      scroll3 = scroll3 + 1;
    

    probably isn't doing what you think it is - the lack of a {} block means that only the scroll1 line is executed when the condition is true. i would suggest to ALWAYS use braces even when you are sure that you want only one line to be conditional. (this is especially true if there's any chance of the indenting messing up, say when you are posting to a forum)

  • edited February 2014 Answer ✓

    this kind of works - you need to move the mouse to change the color-set he is using

    there were a lot of minor errors:

    • for getting num to be 1,2,3 you need num = int(random(3)) + 1; --- with +1 at the end

    • break; must be after } not before (wrong in two places I think)

    • noLoop(); should not be used

    • fill should be before ellipse

    • I have scroll1 += int(e) ; with +=

    • I deleted / uncommented all scroll2 = 0 - 1000;

      int num = 1;
      float e = 0;
      int scroll1 = 0;
      int scroll2 = 0;
      int scroll3 = 0;
      
      void setup() {
        size(700, 650);
        background (111);
      }
      
      
      void draw() { 
        background (111);
        ellipseMode(CENTER);
        ellipse(350, 325, 100, 100);
        // num = int(random(3));
      }
      
      void mouseMoved() {
        num = int(random(3)) + 1;
        println (num+"-------------------------");
      }
      
      void mouseWheel(MouseEvent event) {
        float e = (event.getAmount() + 1);
      
        // if (e == 2.0)
        scroll1 += int(e) ; //This makes sure that the scrolls ints are connected to the mouse roll
        scroll2 += int(e);
        scroll3 += int(e);
      
        while (scroll1>12)
          scroll1=scroll1-11;
      
        while (scroll2>12)
          scroll2=scroll2-11;
      
        while (scroll3>12)
          scroll3=scroll3-11;
      
        // println("     "+scroll1);
        //  println(scroll2);
        //  println(scroll3);
      
        //Scroll sequence
        //--------------
        switch(num) {
      
        case 1: //Green Scroll Sequence
          //scroll2 = 0 - 1000;
          //scroll3 = 0 - 1000;
      
          if (scroll1 == 1) {
            fill(#bde3db);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 2) {
            fill(#9ee6d8);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 3) {
            fill(#41dac5);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 4) {
            fill(#38c8b4);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 5) {
            fill(#25a997);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 6) {
            fill(#1c9786);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 7) {
            ellipse(350, 325, 100, 100);
            fill(#158575);
          }
          else if (scroll1 == 8) {
            fill(#0c6c5e);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 9) {
            fill(#054d43);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 10) {
            fill(#023931);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 11) {
            fill(#012520);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll1 == 12) {
            fill(#000c0a);
            ellipse(350, 325, 100, 100);
      
            // noLoop();
          }
          break;
      
          //-------------
      
        case 2: //Red Scroll Sequence
          //    scroll1 = 0 - 1000;
          //    scroll3 = 0 - 1000;
      
          if (scroll2 == 1) {
            fill(#fff3f5);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 2) {
            fill(#fedadf);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 3) {
            fill(#fdc6ce);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 4) {
            fill(#fab2bc);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 5) {
            fill(#f393a1);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 6) {
            fill(#ea7a8a);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 7) {
            fill(#e36879);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 8) {
            fill(#da5668);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 9) {
            fill(#c7374b);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 10) {
            fill(#be253a);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 11) {
            fill(#611927);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll2 == 12) {
            fill(#421c24);
            ellipse(350, 325, 100, 100);
            // noLoop();
          }
          break;
          //-------------
      
        case 3: //Blue Scroll Sequence
          //    scroll1 = 0 - 1000;
          //    scroll2 = 0 - 1000;
      
          if (scroll3 == 1) {
            fill(#cbecff);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 2) {
            fill(#b0dcf6);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 3) {
            fill(#9acdec);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 4) {
            fill(#81bde2);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 5) {
            fill(#5fa3cc);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 6) {
            ellipse(350, 325, 100, 100);
            fill(#4088b3);
          }
          else if (scroll3 == 7) {
            fill(#2c6e96);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 8) {
            fill(#205a7e);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 9) {
            fill(#0f405f);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 10) {
            fill(#092e46);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 11) {
            fill(#041d2d);
            ellipse(350, 325, 100, 100);
          }
          else if (scroll3 == 12) {
            fill(#010d14);
            ellipse(350, 325, 100, 100);
            // noLoop();
          } // else
          break;
          //
        default:
          println ("unknown with "+num);
          break;
        } // switch
        //
      }
      
  • Works perfectly! Thanks for the help everybody!

Sign In or Register to comment.