Loading...
Logo
Processing Forum
Hi all,
I have the following question:

I have an array of 12 numbers (0 or 1) related to a list of buttons called "paisajeButtons[]". I need to program something that allows me to click only 5 and erase the first one when I click the 6th. 

I have an array of 5 positions in order called "clickOn[]"
From here everything is fine,

if I manually turn off one of the five button, it works at the first step, but it doesn't when I have the second unselect with 4 buttons on. 

I'm trying this but not going well:
Copy code
  1. for (int i = 0; i < 12; i++) {
        if (counterButtons > 0 ) {
          if (paisajeButtons[i] == 0 && (clickOn[0] == i  || clickOn[1] == i  || clickOn[2] == i  || clickOn[3] == i  || clickOn[4] == i )) {
            println("mira si hay un cero en el botón = " + i);
            if (i == clickOn[0]) {
              println("quita el botón 0");
              clickOn[0] = clickOn[1];
              clickOn[1] = clickOn[2];
              clickOn[2] = clickOn[3];
              clickOn[3] = clickOn[4];
              clickOn[4] = -1;
              counterButtons --;
            }
             if (i == clickOn[1]) {
              println("quita el botón 1");
              clickOn[1] = clickOn[2];
              clickOn[2] = clickOn[3];
              clickOn[3] = clickOn[4];
              clickOn[4] = -1;
              counterButtons --;
            }
             if (i == clickOn[2]) {
              println("quita el botón 2");
              clickOn[2] = clickOn[3];
              clickOn[3] = clickOn[4];
              clickOn[4] = -1;
              counterButtons --;
            }
             if (i == clickOn[3]) {
              println("quita el botón 3");
              clickOn[3] = clickOn[4];
              clickOn[4] = -1;
              counterButtons --;
            }
             if (i == clickOn[4]) {
              println("quita el botón 4");
              clickOn[4] = -1;
              counterButtons --;
            }
          }
        }
      }

Replies(2)

That was a good one. I had to do a mental jump to hit on a good solution: Don't just track the button's state, track its clicked on order!
Copy code
  1. class Button {
  2.   int age;
  3.   float px, py;
  4.   int id;
  5.   Button(int iid, float ipx, float ipy) {
  6.     age=0;
  7.     id=iid;
  8.     px=ipx;
  9.     py=ipy;
  10.   }
  11.   void draw() {
  12.     fill((age>0)?255:64);
  13.     stroke(isOver()?color(255, 0, 0):0);
  14.     ellipse(px, py, 20, 20);
  15.   }
  16.   boolean isOver() {
  17.     return(dist(mouseX, mouseY, px, py)<10);
  18.   }
  19.   void click() {
  20.     if ( isOver() && age==0 ) {
  21.       for (int i=0;i<buttons.length;i++) {
  22.         buttons[i].growOld();
  23.       }
  24.       age=5;
  25.     }
  26.   }
  27.   void growOld() {
  28.     if (age>0) {
  29.       age--;
  30.     }
  31.   }
  32. }

  33. Button[] buttons;

  34. void setup() {
  35.   size(220, 220);
  36.   ellipseMode(CENTER);
  37.   buttons = new Button[12];
  38.   for (int i=0;i<buttons.length;i++) {
  39.     buttons[i] = new Button( i, 20 + 30 * (i / 4), 20 + 30 * (i % 4) );
  40.   }
  41. }

  42. void draw() {
  43.   background(128);
  44.   for (int i=0;i<buttons.length;i++) {
  45.     buttons[i].draw();
  46.   }
  47. }

  48. void mousePressed() {
  49.   for (int i=0;i<buttons.length;i++) {
  50.     buttons[i].click();
  51.   }
  52. }
hey man!
That's a very good idea,  thanks a lot!
I'm using ControlP5 but having a parallel class Button that controls the interface is perfect. I should thought this at first glance. great!
Thanks again
David