How can I make a loop inside a toggle button!

In this picture, I want that If I toggle ON the Expert Button, the up, down, left, right keys will start working and if I toggle OFF it then these keys will not work and It will be a loop so that whenever I click button, inside the button the keys will work forever but if it will be off no key will workCapture

Tagged:

Answers

  • I have so much problem with this!!!

  • edited December 2015

    You need a boolean var that tells you whether expert is on or off....

    boolean expertModeIsOn=false;

    now when a arrow is clicked depending on that var do something or not

    (or when expertModeIsOn==false don't show the arrows)

  • edited December 2015
    If (expertModeIsOn) { 
     // arrow does somthing .....
    }
    
  • edited January 2016

    boolean expertModeIsOn=false; boolean Scientist = false; boolean Amateur = false; void setup() { size(800,500); r = 0; g = 0; b = 0;

    noStroke(); cp5 = new ControlP5(this);
    f=loadFont("MinionPro-Semibold-40.vlw"); cp5 = new ControlP5(this); String[] cameras = Capture.list(); PFont pfont = createFont("Arial",15,true); ControlFont font = new ControlFont(pfont,241);

    cp5.addToggle("Expert") .setPosition(80, 150) .setSize(115, 30) .setMode(Toggle.SWITCH);

    cp5.getController("Expert") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15);

    cp5.addToggle("Scientist") .setValue(0) .setPosition(80, 235) .setSize(115, 30) .setMode(Toggle.SWITCH);

    cp5.getController("Scientist") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15); cp5.addToggle("Amateur") .setValue(0) .setPosition(80, 320) .setSize(115, 30) .setMode(Toggle.SWITCH);

    cp5.getController("Amateur") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15); }

    void draw() { fill (0,191,255); triangle(100, 440, 160, 440, 130, 400); triangle(100, 450, 160, 450, 130, 490); triangle(80, 420, 80, 460, 40, 440); triangle(180, 420, 180, 460, 220, 440); fill (0,0,255); textSize (20); fill (0,255,0); textFont(f, 30); fill(0); text("Collision Avoiding", 290, 65); text("Collaborative Control Robot", 230, 95); text("Console", 360, 125);

    void Expert(boolean theFlag) { if(theFlag==false){ println("Hello"); } } void Scientist(boolean theFlag) { if(theFlag==true) { println("Hello"); } } void Amateur(boolean theFlag) { if(theFlag==true) { println("Hello"); } } void keyPressed() { if (expertModeIsOn=true) { switch(keyCode){

    case UP: println("UP!"); fill(255,0,0); triangle(100, 440, 160, 440, 130, 400); break;

    case DOWN: println("DOWN!"); fill(255,0,0); triangle(100, 450, 160, 450, 130, 490); break; case LEFT: println("LEFT!"); fill(255,0,0); triangle(80, 420, 80, 460, 40, 440);
    break;

    case RIGHT: println("RIGHT!"); fill(255,0,0); triangle(180, 420, 180, 460, 220, 440); break;

      default:
      break;
    

    } }; }

  • Heres the programming... now whether the toggle switch is on or off.. the arrow keys are working

  • edited January 2016

    OK.. This runs (I had to comment out a few lines) and does what you want. You only forgot to change the value of expertModeIsOn when switching toggle.

    import controlP5.*;
    
    boolean expertModeIsOn=true; 
    boolean Scientist = false; 
    boolean Amateur = false; 
    PFont pfont;
    int r, g, b;
    ControlP5 cp5;
    
    void setup() { 
      size(800, 500); 
      r = 0; 
      g = 0; 
      b = 0;
    
      noStroke(); 
      cp5 = new ControlP5(this);
      // f=loadFont("MinionPro-Semibold-40.vlw"); 
      cp5 = new ControlP5(this); 
      // String[] cameras = Capture.list(); 
      PFont pfont = createFont("Arial", 15, true); 
      ControlFont font = new ControlFont(pfont, 241);
    
      cp5.addToggle("Expert") .setPosition(80, 150) .setSize(115, 30) .setMode(Toggle.SWITCH);
    
      cp5.getController("Expert") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15);
    
      cp5.addToggle("Scientist") .setValue(0) .setPosition(80, 235) .setSize(115, 30) .setMode(Toggle.SWITCH);
    
      cp5.getController("Scientist") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15); 
      cp5.addToggle("Amateur") .setValue(0) .setPosition(80, 320) .setSize(115, 30) .setMode(Toggle.SWITCH);
    
      cp5.getController("Amateur") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(15);
    }
    
    void draw() { 
      fill (0, 191, 255);
      triangle(100, 440, 160, 440, 130, 400); 
      triangle(100, 450, 160, 450, 130, 490); 
      triangle(80, 420, 80, 460, 40, 440); 
      triangle(180, 420, 180, 460, 220, 440); 
      fill (0, 0, 255); 
      textSize (20); 
      fill (0, 255, 0); 
      //textFont(f, 30); 
      fill(0); 
      text("Collision Avoiding", 290, 65); 
      text("Collaborative Control Robot", 230, 95); 
      text("Console", 360, 125);
    }
    void Expert(boolean theFlag) { 
      if (theFlag==false) { 
        println("Hello");
        expertModeIsOn = true;
      } else {
        expertModeIsOn = false;
      }
    } 
    void Scientist(boolean theFlag) { 
      if (theFlag==true) { 
        println("Hello");
      }
    } 
    void Amateur(boolean theFlag) { 
      if (theFlag==true) { 
        println("Hello");
      }
    } 
    void keyPressed() { 
      if (expertModeIsOn) { 
        switch(keyCode) {
    
        case UP: 
          println("UP!"); 
          fill(255, 0, 0); 
          triangle(100, 440, 160, 440, 130, 400); 
          break;
    
        case DOWN: 
          println("DOWN!"); 
          fill(255, 0, 0); 
          triangle(100, 450, 160, 450, 130, 490); 
          break; 
        case LEFT: 
          println("LEFT!"); 
          fill(255, 0, 0); 
          triangle(80, 420, 80, 460, 40, 440);
          break;
    
        case RIGHT: 
          println("RIGHT!"); 
          fill(255, 0, 0); 
          triangle(180, 420, 180, 460, 220, 440); 
          break;
        default:
          break;
        }
      }
    }
    
  • Amazing!! You did it!!!

Sign In or Register to comment.