How to use cycle through options using booleans and keyPressed?

edited October 2017 in How To...

I am coding a program where I dress up a character, where you have options for 3 different features to choose from so for example, i have chosen eyes, skin color and hat. For my eyes I have 3 different eyes you can pick from. It cycles from one eye to the next using frame = (frame+1)%1260, frame starting at 0. I used this since the frame starts at 0, and i have 7 options to cycle from, cycling for 3 seconds each, so 1260 frames in total. (using framecount to keep track). What I need to do at this point is use booleans so that ONLY feature 1 will cycle until the user presses a key to pick from the options from feature1. once the user picks from feature 1, then feature 2 will start to cycle (feature 1 stays), then once feature 2 is chosen, feature 3 will start to cycle. (dont show the next feature until the previous one is chosen) I just cant figure out how to do that.

Answers

  • edited October 2017

    Study this until you understand how it works then apply that understanding to your own sketch.

    int which; // Which row is active
    int time;  // When to move to the next choice in a row
    int[] choices = {0,0,0}; // Current selections.
    
    void setup(){
      size(300,300);
      which = 0; // top row
      noStroke();
      time = millis() + 1000;  // First new choice in top row happens in one second.
    }
    
    void draw(){
      background(0);
    
      if( millis() > time ){  // Check if it is time for a new choice.
        choices[which]++; // Go to next choice.
        choices[which]%=3; // Loop around to first choice if we need to.
        time = millis() + 1000; // The next new choice happens in one second.
      }
    
      fill(64);
      rect(30,30 + 100*which, 200, 40); // draw the active row indicator.
    
      // Draw each choice square
      for( int j = 0; j < 3; j++ ){
        for( int i = 0; i < 3; i++ ){
          fill(128); // All squares are grey
          if( choices[j] == i ){ // Unless they are the selected choice for the given row
            fill(255,255,0);
          }
          rect(20 +100*i, 20 + 100*j, 60, 60);
        }
      }
    }
    
    void mousePressed(){ // When mouse is pressed,
      which++; // Go to the next row.
      which%=3; // Loop around to top row if we need to.
      time = millis() + 1000; // Reset the timer - next choice happens in a second from NOW.
    }
    
  • i need to use booleans for this not loops :(

  • edited October 2017 Answer ✓

    This should make your brain and eyes hurt. Only an deranged, insane person would do it this way. You really need to learn how to use loops.

    boolean row0, row1, row2;
    boolean pick00, pick01, pick02, pick10, pick11, pick12, pick20, pick21, pick22;
    int time;
    
    void setup() {
      size(300, 300);
      row0 = true;
      pick00 = true;
      pick10 = true;
      pick20 = true;
      noStroke();
      time = millis() + 1000;
    }
    
    void draw() {
      background(0);
      if (millis()>time) {
        if (row0) {
          if (pick00) {
            pick00 = false;
            pick01 = true;
          } else if (pick01) {
            pick01 = false;
            pick02 = true;
          } else {
            pick02 = false;
            pick00 = true;
          }
        } else if (row1) {
          if (pick10) {
            pick10 = false;
            pick11 = true;
          } else if (pick11) {
            pick11 = false;
            pick12 = true;
          } else {
            pick12 = false;
            pick10 = true;
          }
        } else {
          if (pick20) {
            pick20 = false;
            pick21 = true;
          } else if (pick21) {
            pick21 = false;
            pick22 = true;
          } else {
            pick22 = false;
            pick20 = true;
          }
        }
        time = millis() + 1000;
      }
    
      fill(64);
      if (row0) {
        rect(30, 30, 200, 40);
      }
      if (row1) {
        rect(30, 130, 200, 40);
      }
      if (row2) {
        rect(30, 230, 200, 40);
      }
    
      fill(128);
      rect(20, 20, 60, 60);
      rect(120, 20, 60, 60);
      rect(220, 20, 60, 60);
      rect(20, 120, 60, 60);
      rect(120, 120, 60, 60);
      rect(220, 120, 60, 60);
      rect(20, 220, 60, 60);
      rect(120, 220, 60, 60);
      rect(220, 220, 60, 60);
    
      fill(255, 255, 0);
      if (pick00) { 
        rect(20, 20, 60, 60);
      }
      if (pick01) { 
        rect(120, 20, 60, 60);
      }
      if (pick02) { 
        rect(220, 20, 60, 60);
      }
      if (pick10) { 
        rect(20, 120, 60, 60);
      }
      if (pick11) { 
        rect(120, 120, 60, 60);
      }
      if (pick12) { 
        rect(220, 120, 60, 60);
      }
      if (pick20) { 
        rect(20, 220, 60, 60);
      }
      if (pick21) { 
        rect(120, 220, 60, 60);
      }
      if (pick22) { 
        rect(220, 220, 60, 60);
      }
    
    }
    
    void mousePressed() {
      if (row0) {
        row0 = false;
        row1 = true;
      } else if (row1) {
        row1 = false;
        row2 = true;
      } else {
        row2 = false;
        row0 = true;
      }
      time = millis() + 1000;
    }
    

    Do not study this. It is awful. Try to understand why this code is so terrible, and then apply that understanding as motivation to learn all about arrays and loops.

  • yeah thank you! im actually just doing a comparison for a project lol, how would i get it to stop once i've pressed which options i've wanted?

  • First, understand what the above code is doing.

    Second, remove the line that causes it to go back to the first row.

    This will introduce a bug, as the above code assumes you are on the third row if you are not on the first or second row. Add additional checks to make sure you are on the third row to fix this.

Sign In or Register to comment.