Combine KeyPressed and Switch with different behavior?

edited April 2014 in How To...

Hi, I would like to control two different interactions through keyPressed in one switch. One is a rotating windmill which can be switch on and off. The other 4 different circles added or removed through keyPressed. The interactions work individually but not when I combine them in a switch. Is there a better way to do that? Thank you for your help.

boolean rotation1 = false, rotation2 = false; // boolean windmill
boolean drawingFirstEllipse, drawingSecondEllipse, drawingThirdEllipse, drawingFourthEllipse = true;

void draw(){ 

 //windmill ON/OFF
...
    }

    // draw cloud
      if(drawingFirstEllipse) ellipse(50, 50, 10, 10);
      if(drawingSecondEllipse) ellipse(50, 50, 70, 10);
      if(drawingThirdEllipse) ellipse(50, 50, 130, 10);
      if(drawingFourthEllipse) ellipse(50, 50, 190, 10);
}
void keyPressed() {

  switch(interaction){

   case 1: 

   // windmill ON/OFF
       if (key == '1')
       rotation1 = !rotation1;  

       else if (key == '2') 
       rotation2 = !rotation2; 

     // cloud behavior

     case 2: 
     if (key == '3')
     drawingFirstEllipse = !drawingFirstEllipse;
     break;

     case 3:
     if (key == '4')
     drawingSecondEllipse = !drawingSecondEllipse;
     break;

     case 4:
     if (key == '5')
     drawingThirdEllipse = !drawingThirdEllipse;
     break;

     case 5:
     if (key == '6')
     drawingFourthEllipse = !drawingFourthEllipse;
     break;

     default:
     if (key == '7')
        interaction = 2;
  }

Answers

  • edited April 2014

    Why have you got an undeclared variable for -> interaction?
    Please post a compilable code! [..]

  • Sorry, now I post you the entire code. Thank you.

    //Declaration variables
    PImage windmillBase;
    PImage windmillWheel;
    
    boolean rotation1 = false, rotation2 = false; // boolean windmill
    boolean drawingFirstEllipse, drawingSecondEllipse, drawingThirdEllipse, drawingFourthEllipse = true;
    
    //cloud cloud;
    int interaction = 0;
    windmill windmill;
    windmill2 windmill2;
    
    void setup()
    
      {
        size (840,595);
        //draw background  
        background(#BCFCA1);
        smooth();
    
         // Load images
         windmillBase=loadImage("pic_windmill_1.png");
         windmillWheel=loadImage("pic_windmill_2.png");
    
         // call classes + parameter
         windmill = new windmill (550, 500); // new instanz windmill base 1
         windmill2 = new windmill2 (700, 500); // new instanz windmill 2
    
      }
    
    void draw(){ 
    
        imageMode(CENTER);
    
        //windmill ON
        if (rotation1){
    
        //  windmill 1
    
          windmill.drawWindmill();
          windmill.rotationWindmill();
    
    
        // windmill 2  
          windmill2.drawWindmill2(); 
          windmill.rotationWindmill2();
    
        // overlapping windmill base
          windmill.drawWindmillCopy(); 
          windmill2.drawWindmill2Copy(); 
          loop();
            }
    
        //windmill OFF
        else if (rotation2) {
    
        //windmill 1
          windmill.drawWindmill();
          windmill.rotationWindmill();
    
        //windmill 2 
          windmill2.drawWindmill2(); 
          windmill.rotationWindmill2();
          noLoop();
        }
    
        // draw cloud
          if(drawingFirstEllipse) ellipse(50, 50, 100, 100);
          if(drawingSecondEllipse) ellipse(50, 50, 70, 10);
          if(drawingThirdEllipse) ellipse(50, 50, 130, 10);
          if(drawingFourthEllipse) ellipse(50, 50, 190, 10);
    
    }
    
    
    
     // behaviour windmill 
    
    
    void keyPressed() {
    
      switch(interaction){
    
       case 1: 
    
       // windmill ON/OFF
           if (key == '1')
           rotation1 = !rotation1;  
    
           else if (key == '2') 
           rotation2 = !rotation2; 
    
         // cloud behavior
    
         case 2: 
         if (key == '3')
         drawingFirstEllipse = !drawingFirstEllipse;
         break;
    
         case 3:
         if (key == '4')
         drawingSecondEllipse = !drawingSecondEllipse;
         break;
    
         case 4:
         if (key == '5')
         drawingThirdEllipse = !drawingThirdEllipse;
         break;
    
         case 5:
         if (key == '6')
         drawingFourthEllipse = !drawingFourthEllipse;
         break;
    
         default:
         if (key == '7')
            interaction = 2;
      }
    
    
    }
    ////////////////////////////////////////
    public class windmill
    
    {
    
    int xpos;
    int ypos;
    
    //declaration for rotation
    float angle; 
    
    
     // constructor windmill base 1
      windmill(int windmillx, int windmilly){
      xpos = windmillx;
      ypos = windmilly;
    
    }
    
    
    void drawWindmill(){
    
    //draw windmill base to screen
    image(windmillBase,xpos,ypos);
    }
    
    
    void rotationWindmill()
      {
        pushMatrix();
        fill(#BCFCA1);
        noStroke();
        rect(500,390,100,100);
        translate(549,442); // define center
        rotate(radians(angle));
        image(windmillWheel,0,0,95,95);
        angle += 1;
        popMatrix();
        loop();
      }
        // windmill 2
      void rotationWindmill2()
      {
        pushMatrix();
        fill(#BCFCA1);
        noStroke();
        rect(650,390,100,100);
        translate(699,442); // define center
        rotate(radians(angle));
        image(windmillWheel,0,0,95,95);
        angle += 1;
        popMatrix();
      }
    }
    
    
    ///////////////////////////////
    public class windmill2
    
    {
    int xpos2;
    int ypos2;
    
    //constructor windmill base 1
      windmill2 (int windmill2x, int windmill2y){
      xpos2 = windmill2x;
      ypos2 = windmill2y;
    }
    
    void drawWindmill2(){
    
    //draw windmill base to screen
    image(windmillBase,xpos2,ypos2);
    }
    
    }
    
  • edited April 2014
    • Variable interaction starts out w/ 0.
    • Inside switch, it's gonna default to if (key == '7')
    • Once key 7 is pressed, it's gonna case 2:for if (key == '3').
    • After that, hitting 3 gonna keep on drawingFirstEllipse = !drawingFirstEllipse;.
    • The end!
Sign In or Register to comment.